- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 1gb 长数组,其中包含 .bin 文件中的 float 。我读完后如何用 avx2 指令对元素求和并打印结果?
我用 Jake 'Alquimista' LEE 的答案编辑了我的代码。问题是结果比实际要小得多。还有另一个问题,如何为从 .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;
}
最佳答案
将 1GB 文件读入内存是很大的内存和 I/O 开销。虽然我对AVX2
不是很熟悉,我阅读了互联网上的文章,我可以提出以下解决方案,该解决方案经过实际测试并证明是有效的。
我的解决方案包括将文件读取为 512 字节的 block (128 个 float 的 block ),然后对 vector 对(每个 block 总共 16 个 vector )求和,以便最后我们得到最终的 __m256
vector ,通过将其转换为 float*
我们可以总结其各个组成部分以获得最终结果。
文件未128浮点对齐的情况在最后for
中处理。通过对各个浮点求和来循环。
代码已注释,但如果您有任何建议为答案添加更多解释,请随意这样做。
#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*)∑ /* 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;
}
关于c - AVX2 1GB长阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47105601/
#include typedef std::vector vc; typedef std::vector vvc; vvc f() { const int N = (1 不把它还给操作系统。 因
我正在尝试训练 Dlib's train_shape_predictor_ex.cpp与 Halen数据集。我在 Release模式 中使用 Visual Studio 将代码编译为 64 位平台 作
当我试图解决this 时想到了这个问题。问题。 我有一个容量为 120 GB 的硬盘,其中 100 GB 被一个巨大的文件占用。所以 20 GB 仍然是免费的。 我的问题是,我们如何将这个巨大的文件拆
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Can you allocate a very large single chunk of memory (
我有一台运行 Linux (3.19.5-200.fc21.x86_64) 的 128 GB RAM 的计算机。但是,我不能在单个进程中分配超过 ~30 GB 的 RAM。除此之外,malloc 失败
我需要为 808704000 个 float 分配内存,大约是 3085 MB。我的电脑有 32 GB 内存,运行 64 位 Linux (CentOS 6.6)。每次我尝试分配内存时,malloc
很抱歉这个愚蠢的问题,但是当reading about 32 bits limitation ,我发现由于 Memory-Mapped 方法,MongoDB 不能存储超过 2 GB,但是: 2^32
假设我们有一个 32 位地址,那么每一位可以是 1 或 0。 所以组合总数等于2^32。 所以我们可以表示 2^32 个地址(没有单位)。 但为什么人们说 32 位地址可以表示 2^32 字节地址(为
好吧,这个问题确实是一个挑战! 背景 我正在从事一个涉及比正常数字更大的基于算术的项目。我是新手,我打算使用 4 GB 文件大小的最坏情况(我什至希望将其扩展到 5GB 上限,因为我之前看到文件大小大
我在文件系统上有大约 12 个大小为 1 GB 到 10 GB 的存储库,我需要为所有这些存储库设置自动备份(我们的旧备份脚本在计算机出现故障时丢失了) XP 64 位机器。 看完this quest
我尝试调整 Linux VM 上的操作系统磁盘大小,该 VM 通常由 azure 自动创建。我无法创建自定义操作系统磁盘 - 请告知我该怎么做? enter image description her
我在 .NET 中遇到了一个问题,我的数组受到我拥有的 RAM 数量的限制,并且我需要可以容纳至少 40 GB 字节的数组。我正在考虑使用硬盘驱动器作为虚拟数组的想法不在乎它是否慢。 我正在研究这个想
我尝试调整 Linux VM 上的操作系统磁盘大小,该 VM 通常由 azure 自动创建。我无法创建自定义操作系统磁盘 - 请告知我该怎么做? enter image description her
我的目标是看看当使用比物理 GPU 内存所能容纳的更多纹理数据时会发生什么。我的第一次尝试是加载多达 40 个 DDS 纹理,导致内存占用比 GPU 内存高得多。但是,我的场景在 9500 GT 上仍
Windows 上的 JDK 最多需要 2 GB 左右的 RAM。即使我们为 JDK 分配更多 RAM;它不接受它。如果我需要在 Windows 上运行需要 8 GB RAM 的进程;我怎样才能实现它
我有一个程序需要分配2个15亿长度的整数数组。这是一个编码挑战( https://projecteuler.net/problem=282 ),并且没有办法使用如此大的数组(如果有,请不要告诉我;我应
假设我有一个 32 位内核。 4 Gb RAM,10 Gb 交换分区。 我有一个在无限循环中有 malloc 的进程。因此,最终系统的 OOM 将终止该进程。这里有两个论点。 参数 1:因为它是 32
我有一个可以大于 4GB 的文件。我正在使用 linux split 命令按行拆分它(这是要求)。但是拆分原始文件后,我希望拆分文件的大小始终小于 2GB。原始文件大小可能在 3-5 GB 之间。我想
我有一台带有 32GB RAM 的 Mac 服务器(雪豹)。当我尝试在 Perl (v 5.10.0) 中分配超过 1.1GB 的 RAM 时,出现内存不足错误。这是我使用的脚本: #!/usr/bi
我们正在尝试运行.NET 4 Web应用程序(在8 GB,8核心Windows 2008 R2 64位Webedition服务器上),该应用程序使用Spire.doc创建Word mailmerge文
我是一名优秀的程序员,十分优秀!