AVX2 1GB长阵列

时间:2017-11-03 23:09:39

标签: c avx2

我有一个1GB的长数组,在.bin文件中有浮点数。在我阅读之后,我如何用avx2指令对元素求和,并打印结果?

我用Jake' Alquimista'编辑了我的代码。李的回答。 问题是结果比它要小得多。还有一个问题,我如何为每个从.bin文件中提取的数字添加常量?

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <immintrin.h>

inline float sumf(const float *pSrc, uint32_t len)
{
    __m256 sum, in;
    float sumr;
    uint32_t sumi;
    uint32_t lenr = len & 7;
    while (len--)
    len >>= 3;
    sum = _mm256_set1_ps(0.0f);
    {
        in = _mm256_loadu_ps(pSrc++);
        sum = _mm256_add_ps(in, sum);
    }

    sum = _mm256_hadd_ps(sum, in);
    sum = _mm256_hadd_ps(sum, in);
    sum = _mm256_hadd_ps(sum, in);
    sumi = _mm256_extract_epi32(*(__m256i *)&sum, 0);
    sumr = *(float *)&sumi;

    while (lenr--)
    {
        sumr += *pSrc++;
    }

    return sumr;
}


int main(void)
{

        FILE *file;

        float *buffer2;
        uint32_t fileLen;

        if((file = fopen("example.bin","rb"))==NULL)
        {
                printf("Error! opening file");
                exit(1);
        }


        fseek(file, 0, SEEK_END);
        fileLen=ftell(file);
        fseek(file, 0, SEEK_SET);
    buffer2=(float *)malloc(fileLen+1);
        if (!buffer2)
        {
                fprintf(stderr, "Memory error!");
                                fclose(file);
                return 0;
        }


        fread(buffer2, fileLen, 1, file);
        fclose(file);
        printf( "File size : %lu Bits \n", fileLen );
        for(int i = 0; i<10; i++)
        printf("%f \n", buffer2[i]);

    float sum =sumf(buffer2,fileLen);
        printf("%f\n",s);
        free(buffer2);
        return 0;
}

3 个答案:

答案 0 :(得分:0)

inline float sumf(const float *pSrc, uint32_t len)
{
    __m256 sum, in;
    float sumr;
    uint32_t sumi;
    uint32_t lenr = len & 7;
    len >>= 3;
    sum = _mm256_set1_ps(0.0f);
    while (len--)
    {
        in = _mm256_loadu_ps(pSrc++);
        sum = _mm256_add_ps(in, sum);
    }
    in =  *(__m256 *)&_mm256_permute4x64_pd(*(__m256d *)&sum, 0b01001110);
    sum = _mm256_hadd_ps(sum, in);
    sum = _mm256_hadd_ps(sum, in);
    sum = _mm256_hadd_ps(sum, in);
    sumi = _mm256_extract_epi32(*(__m256i *)&sum, 0);
    sumr = *(float *)&sumi;

    while (lenr--)
    {
        sumr += *pSrc++;
    }

    return sumr;
}

上面的功能可以。但是,我不认为它会带来很大的性能提升,因为它是一个非常微不足道的,并且编译器无论如何都会自动对其进行矢量化。

请注意,当您将指针作为参数传递时,必须将指针指向float *并将filelen除以sizeof(float)

答案 1 :(得分:0)

这里(很有可能)你的错误:

while (len--)
len >>= 3;

那是一段时间的循环。只要len!= 0,就用len替换len(len - 1)&gt;&gt; 3.然后将其更改为-1。没有循环可见。

答案 2 :(得分:0)

将1GB文件读入内存是一个很大的内存和I / O开销。虽然我对AVX2不是很熟悉,但我读过互联网和文章中的文章。我可以提出以下解决方案,该方案经过实际测试并证明有效。

我的解决方案包括将文件读取为512字节的块(128个浮点数块),然后对向量对进行求和(每个块16个向量),最后通过转换得到最终的__m256向量它到float*我们可以总结其各个组成部分以获得最终结果。

通过对各个浮点数求和,在最后for循环中处理文件未对齐128个浮点数的情况。

代码已注释,但如果您有任何建议可以为答案添加更多解释,请随时这样做。

#include <immintrin.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int     make_floatf(char *, int);
float   avx_sfadd(char*);

char error_buf[1024];

#define PERROR()                            \
    do {                                    \
        strerror_r(errno, error_buf, 1024); \
        printf("Error: %s\n", error_buf);   \
        fclose(fp);                         \
        return -1;                          \
    } while(0)

/* This function generates a .bin file containing blocks 
 *   of 128 floating point numbers
 */
int make_floatf(char *filename, int nblocks)
{
    FILE *fp = NULL;

    if(!(fp = fopen(filename, "wb+")))
        PERROR();

    float *block_ptr = malloc(sizeof(float) * 128);  /* 512 Bytes block of 128 floats */
    if(!block_ptr)
        PERROR();

    int j, i;

    for(j = 0; j < nblocks; j++)
    {
        for(i = 0; i < 128; i++)
            block_ptr[i] = 1.0;

        int ret = fwrite(block_ptr, sizeof(float), 128, fp);
        if(ret < 128)
        {
            free(block_ptr);
            PERROR();
        }
    }

    free(block_ptr);
    fclose(fp); 

    return 0;
}

/* This function reads the .bin file as chuncks of 512B 
 * blocks (128 floating point numbers) and calculates thier sum.
 * The final sum in a form of vector is looped through and its 
 * components are summed up to get the final result.
 */
float avx_sfadd(char *filename)
{
    FILE *fp = NULL;

    __m256  v1;
    __m256  v2;
    __m256  sum = _mm256_setzero_ps();

    if(!(fp = fopen(filename, "rb")))
       PERROR();

    struct stat stat_buf;
    stat(filename, &stat_buf);

    size_t fsize     = stat_buf.st_size;
    size_t nblocks   = fsize / (sizeof(float) * 128); 
    size_t rem_size  = fsize - nblocks * sizeof(float) * 128;
    size_t rem_floats = rem_size / (sizeof(float));

    printf("File size: %ld\nnblocks:%ld\nnremfloats: %ld\n",\
            fsize, nblocks, rem_floats); 

    /* This memory area will hold the 128 floating point numbers per block */
    float *block_ptr = malloc(sizeof(float) * 128);
    if(!block_ptr)
        PERROR();

    int i;
    for(i = 0; i < nblocks; i++)
    {
        int ret = fread(block_ptr, sizeof(float), 128, fp);
        if(ret < 128)
            PERROR();   

        /* Summing up vectors in a block of 16 vectors (128 floats) */
        int j;
        for(j = 0; j < 16; j += 2)
        {
            v1 = _mm256_loadu_ps(block_ptr + j*8);
            v2 = _mm256_loadu_ps(block_ptr + (j+1)*8);

            sum += _mm256_add_ps(v1, v2);
        } 
    }

    /* Handling the case if the last chunck of the file doesn't make 
     * a complete block.
     */
    float rem_sum = 0;
    if(rem_size > 0)
    {
        int ret = fread(block_ptr, 1, rem_size, fp);
        if(ret < rem_floats)
            PERROR();

        int j;
        for(j = 0; j < rem_floats; j++)
            rem_sum += block_ptr[j];
    }

    float final_sum = rem_sum;
    float *sum_ptr = (float*)&sum; /* The final vector hold the sum of all vectors */

    /* Summing up the values of the last vector to get the final result */
    int k;
    for(k = 0; k < 8; k++)
        final_sum += sum_ptr[k];

    free(block_ptr);
    fclose(fp);

    return final_sum;
}


int main(int argc, char **argv)
{
    if(argc < 2){
        puts("./main filename [nblocks]");
        return 0;
    }

    /* ./main filename number_of_block_to_create (eg. ./main floats.bin 1024 )*/
    else if(argc == 3){

        if(!make_floatf(argv[1], atoi(argv[2])))
            puts("File has been created sucessfully\n");
    }

    /* ./main filename (eg. ./main floats.bin) to calculate sum*/
    else 
        printf("avx_sum = %f\n", avx_sfadd(argv[1])) :


    return 0;
}