I've written a prime sieve program in c++, which uses ~12GB ram to calculate all primes below 100,000,000,000 (100 Billion).
我用c ++编写了一个主筛程序,它使用~12GB ram来计算低于100,000,000,000(1000亿)的所有素数。
The program works fine when compiled with Visual Studio 2012 (in a project set up for x64) as well as g++ on 64 bit linux. However, when compiled with g++ in cygwin64 on Windows 7 Home Premium 64 bit, a segmentation fault occurs when attempting to use more than ~2GB ram (running the sieve for > ~17,000,000,000)
使用Visual Studio 2012(在为x64设置的项目中)以及64位Linux上的g ++编译时,该程序工作正常。但是,当在Windows 7 Home Premium 64位上用cygwin64中的g ++编译时,尝试使用超过~2GB的ram(运行筛子> ~17,000,000,000)时会发生分段错误
I'm fairly sure it's running as a 64 bit process as there's no *32 next to the process name in task manager.
我很确定它是作为64位进程运行的,因为任务管理器中的进程名称旁边没有* 32。
The code:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
long long sieve(long long n);
int main(int argc, char** argv) {
const long long ONE_BILLION = 1000*1000*1000;
if(argc == 2)
cout << sieve(atol(argv[1])) << endl;
else
cout << sieve(ONE_BILLION * 100) << endl;
}
long long sieve(long long n) {
vector<bool> bools(n+1);
for(long long i = 0; i <=n; i++)
bools[i] = true;
double csqrtn = sqrt(n);
for (long long i = 2; i < csqrtn; ++i)
if (bools[i])
for (long long j = i * i; j < n; j += i)
bools[j] = false;
long long primes2 = 0;
for (long long i = 2; i < n; i++)
if (bools[i])
primes2++;
return primes2;
}
Working fine in Visual studio:
在Visual Studio中工作正常:
Working fine on x64 linux:
在x64 linux上正常工作:
Compiled with the command:
用命令编译:
$ g++ -O3 sieve.cpp -o sieve.exe
$ g ++ -O3 sieve.cpp -o sieve.exe
Running for 18 billion fails:
运行180亿失败:
$ ./sieve.exe 18000000000
Segmentation fault (core dumped)
Works fine (using 2,079,968 K memory according to task manager, though my reputation doesn't allow me to post a third link.)
工作正常(根据任务经理使用2,079,968 K内存,虽然我的声誉不允许我发布第三个链接。)
$ ./sieve.exe 17000000000
755305935
g++ version:
$ g++ --version
g++ (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Note: if you are going to try and run this yourself, it can take quite a long time. On a 3570k @ 4.2GHz running 100 billion in visual studio takes around 30 mins, 1 billion around 10 seconds. However you might be able to duplicate the error with just the vector allocation.
注意:如果您打算尝试自己运行,可能需要很长时间。在3570k @ 4.2GHz运行1000亿视觉工作室需要大约30分钟,10亿大约10秒。但是,您可以仅使用向量分配来复制错误。
Edit: since I didn't explicitly put a question: Why does this happen? Is it a limitation of the cygwin64 dll (cygwin64 was only released fully about a month ago)?
编辑:因为我没有明确提出一个问题:为什么会发生这种情况?它是cygwin64 dll的限制(cygwin64仅在一个月前完全发布)?
1 个解决方案
#1
0
Try increasing the cygwin memory limit. This cygwin documentation suggests that the default maximum application heap size on 64-bit platforms is 4GB... although, this may be referring to 32-bit executables on 64-bit platforms... not sure what restrictions cygwin64 64-bit applications would have regarding their maximum heap size.
尝试增加cygwin内存限制。这个cygwin文档表明64位平台上的默认最大应用程序堆大小是4GB ......虽然这可能是指64位平台上的32位可执行文件...不确定cygwin64 64位应用程序会有什么限制关于他们的最大堆大小。
#1
0
Try increasing the cygwin memory limit. This cygwin documentation suggests that the default maximum application heap size on 64-bit platforms is 4GB... although, this may be referring to 32-bit executables on 64-bit platforms... not sure what restrictions cygwin64 64-bit applications would have regarding their maximum heap size.
尝试增加cygwin内存限制。这个cygwin文档表明64位平台上的默认最大应用程序堆大小是4GB ......虽然这可能是指64位平台上的32位可执行文件...不确定cygwin64 64位应用程序会有什么限制关于他们的最大堆大小。