[英]mmap of 4k * 4K floats leading to segmentation fault
I'm reading matrix of floats from file. 我正在从文件读取浮点矩阵。 Dimensions of matrix are 4k * 4K.
矩阵尺寸为4k * 4K。 With the below program, it just leads to reset in now() function strangely.
使用下面的程序,它只是导致奇怪地重置now()函数。 if I reduce the matrix size to 1k * 1K, it doesn't reset.
如果我将矩阵大小减小到1k * 1K,它不会重置。 Though it reads floats correctly but last few values are junk.
尽管它正确读取浮点数,但最后几个值是垃圾。 I don't know where these junk values are coming from.
我不知道这些垃圾值来自何处。 I took the size of BUFFSIZE 6 since it digits in the floating number is going to be around 5-6.
我选择了BUFFSIZE 6的大小,因为该数字在浮动数字中大约为5-6。 Not sure if it is correct.
不确定是否正确。
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#define ROWS 4000
#define COLS 4000
#define BUFFSIZE 6
//#define USE_FREAD
#define USE_MMAP
double now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.;
}
int main()
{
double end_time;
double total_time;
int i, x, y, k;
for (k = 0; k < 1; k++)
{
double start_time = now();
FILE* in = fopen("resistence_file", "rb");
float arr[ROWS][COLS];
char temp[BUFFSIZE];
int val;
std::stringstream ss;
char* floats = (char*)mmap(
0,
ROWS * COLS * sizeof(float),
PROT_READ,
MAP_FILE | MAP_PRIVATE,
fileno(in),
0
);
fclose(in);
ss<<floats;
for (int i =0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if ((ss.getline(temp, BUFFSIZE, ' ')) )
{
arr[i][j] = atof((temp));
}
}
}
for (int i =0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%.1f ", arr[i][j]);
}
printf("\n");
}
munmap(floats, ROWS * COLS * sizeof(float));
end_time = now();
total_time = end_time - start_time;
printf("It took %f seconds to read %d * %d matrix \n", total_time, ROWS, COLS);
}
return 0;
}
float arr[4000][4000];
will require 56Mb (assuming sizeof(float)=4
). 将需要56Mb(假设
sizeof(float)=4
)。 This is very likely to be larger than your available stack. 这很可能大于您的可用堆栈。
You'll need to either move arr
to have static duration 您需要移动
arr
来保持静态持续时间
static float arr[ROWS][COLS];
...
int main()
or allocate it dynamically, remembering to free
it later 或动态分配它,记得以后再
free
它
int main()
{
float (*arr)[COLS] = malloc(sizeof(*arr) * ROWS);
....
free(arr);
sizeof(float)*K
bytes as if it contained binary data. sizeof(float)*K
字节的文件,就好像它包含二进制数据一样。 If it contains known amount of binary floating point data, why are you trying to work with later it as if it contained text? sizeof(float)
? sizeof(float)
什么关系? Are you absolutely sure your floating-point numbers occupy, as text , sizeof(float)
bytes on the average, including the space delimiter? sizeof(float)
包括空格定界符sizeof(float)
个字节? That's about 3 non-blank characters per number at most places. ss
a string which may or may not be NUL-terminated. ss
可能不为NUL终止的字符串。 Not directly related to the crash: this whole business of stringstream
and getline
here is a complete waste of spacetime (OK at least a waste of 64 megabytes of space and of at least some time). 与崩溃没有直接关系:这里的整个
stringstream
和getline
业务都是对时空的完全浪费(可以,至少浪费64兆字节的空间,至少要浪费一些时间)。 Not speaking of atof
which no programmer should ever touch with a six feet long pole. atof
程序员不应该用六英尺长的杆触碰到的顶点。 I hope you don't seriously plan to do that in production code. 希望您不要认真计划在生产代码中这样做。 Just use
std::strtod
on the original array in place of all this. 只需在原始数组上使用
std::strtod
代替所有这些即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.