数字过高会导致段错误
这段代码来自我正在编写的一个程序,该程序接受 x 列和 x 行以在 CUDA 上运行矩阵乘法,并行处理。样本量越大越好。
我有一个自动生成 x 个随机数的函数。
我知道答案很简单,但我只是想知道到底为什么。但是当我使用数组中的 625000000 个元素运行它时,它会出现段错误。我认为这是因为我已经超出了内存中允许的 int 大小。
对于更大的数字,我应该使用什么数据类型来代替 int? 这就是数据分配然后传递给函数的方式。
a.elements = (float*) malloc(mem_size_A);
已
int mem_size_A = sizeof(float) * size_A; //for the example let size_A be 625,000,000
通过:
randomInit(a.elements, a.rowSize,a.colSize, oRowA, oColA);
randomInit 正在做的事情是说我输入 2x2,但我将其填充到 16 的倍数。因此,它采用 2x2 并将矩阵填充到 16x16 的零,并且 2x2 仍然存在。
void randomInit(float* data, int newRowSize,int newColSize, int oldRowSize, int oldColSize)
{
printf("Initializing random function. The new sized row is %d\n", newRowSize);
for (int i = 0; i < newRowSize; i++)//go per row of new sized row.
{
for(int j=0;j<newColSize;j++)
{
printf("This loop\n");
if(i<oldRowSize&&j<oldColSize)
{
data[newRowSize*i+j]=rand() / (float)RAND_MAX;//brandom();
}
else
data[newRowSize*i+j]=0;
}
}
}
我什至在循环中使用 printf 运行它。这是我得到的结果:
Creating the random numbers now
Initializing random function. The new sized row is 25000
This loop
Segmentation fault
This bit of code is from a program I am writing to take in x col and x rows to run a matrix multiplication on CUDA, parallel processing. The larger the sample size, the better.
I have a function that auto generates x amount of random numbers.
I know the answer is simple but I just wanted to know exactly why. But when I run it with say 625000000 elements in the array, it seg faults. I think it is because I have gone over the size allowed in memory for an int.
What data type should I use in place of int for a larger number?
This is how the data is being allocated, then passed into the function.
a.elements = (float*) malloc(mem_size_A);
where
int mem_size_A = sizeof(float) * size_A; //for the example let size_A be 625,000,000
Passed:
randomInit(a.elements, a.rowSize,a.colSize, oRowA, oColA);
What the randomInit is doing is say I enter a 2x2 but I am padding it up to a multiple of 16. So it takes the 2x2 and pads the matrix to a 16x16 of zeros and the 2x2 is still there.
void randomInit(float* data, int newRowSize,int newColSize, int oldRowSize, int oldColSize)
{
printf("Initializing random function. The new sized row is %d\n", newRowSize);
for (int i = 0; i < newRowSize; i++)//go per row of new sized row.
{
for(int j=0;j<newColSize;j++)
{
printf("This loop\n");
if(i<oldRowSize&&j<oldColSize)
{
data[newRowSize*i+j]=rand() / (float)RAND_MAX;//brandom();
}
else
data[newRowSize*i+j]=0;
}
}
}
I've even ran it with the printf in the loop. This is the result I get:
Creating the random numbers now
Initializing random function. The new sized row is 25000
This loop
Segmentation fault
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对
data
的内存分配可能失败。幸运的是,您几乎肯定不需要存储大量随机数。
运行
对于一些
n
的巨大集合,您可以:当您需要时 ,您可以运行:一个特定的数字,你每次都会得到相同的值,就好像它们都是预先计算好的一样。
Your memory allocation for
data
is probably failing.Fortunately, you almost certainly don't need to store a large collection of random numbers.
Instead of storing:
for some huge collection of
n
, you can run:when you need a particular number and you'll get the same value every time, as if they were all calculated in advance.
我认为您已经超过了为
data
分配的值。当 newrowsize 太大时,您正在访问未分配的内存。请记住,
数据
并不是无限大。I think you're going past the value you allocated for
data
. when you're newrowsize is too large, you're accessing unallocated memory.remember,
data
isn't infinitely big.真正的问题是,如果问题确实是用于数组访问的整数大小,那么您将无法修复它。我认为您的内存中可能没有足够的空间来存储如此大量的数据。
如果您想扩展它,只需定义一个自定义结构或类(如果您使用的是 C++)。但是您将失去数组涉及的 O(1) 时间访问复杂性。
Well the real problem is that, if the problem is really the integer size used for your array access, you will be not able to fix it. I think you probably just have not enough space in your memory so as to store that huge number of data.
If you want to extends that, just define a custom structure or class if you are in C++. But you will loose the O(1) time access complexity involves with array.