gpt4 book ai didi

c - Sieve Of Eratosthenes BSP C 实现没有找到所有素数

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:34 25 4
gpt4 key购买 nike

我有一个 C 语言的 BSP 实现,用于 Erastothenes 筛法,请参见下面的代码。

当使用 ./bspsieve 2 100 执行时,它会给出以下输出:

“2 次中的 0 次触发花费了 0.000045 秒。23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,"

对于 ./bspsieve 1 100 它给出相同的结果,即:"./bspsieve 1 100
proc 0 out of 1 花费了:0.000022 秒。23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,"

对于 ./bspsieve 8 100(因此使用 8 个处理器),它给出了段错误。IE"./bspsieve 8 100它花费了:0.000146 秒 proc 0 out of 8。段错误(核心已转储)”我认为这意味着我的边界不正常?

它找不到第一个素数!我找不到我的错(真的没有 C 经验)。除此之外,你们可以建议对我的代码进行其他改进吗?该算法不需要很快,但欢迎在可理解性和可读性方面进行任何改进。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mcbsp.h>


/*
Note: To compile, this file has to be in the same folder as mcbsp.h and you need the 2 following commands:
gcc -Iinclude/ -pthread -c -o bspsieve.o bspsieve.c
gcc -o bspsieve bspsieve.o lib/libmcbsp1.1.0.a -lpthread -lrt

*/

int procs;
int upperbound;
int *primes;

//SPMD function
void bspSieve(){
bsp_begin(procs);

int p = bsp_nprocs(); // p = number of procs obtained
int s = bsp_pid(); // s = proc number

float blocksize; // block size to be used, note last proc has a different size!
if( s != p-1){
blocksize = ceil(upperbound/p);
} else {
blocksize = upperbound - (p-1)*ceil(upperbound/p);
}

// Initialize start time and end time, set start time to now.
double start_time,end_time;
start_time = bsp_time();

// Create vector that has block of candidates
int *blockvector;
blockvector = (int *)malloc(blocksize*sizeof(int));
int q;
for(q = 0; q<blocksize; q++){
//List contains the integers from s*blocksize till blocksize + s*blocksize
blockvector[q] = q + s*blocksize;
}

//We neglect the first 2 'primes' in processor 0.
if(s == 0){
blockvector[0] = 0;
blockvector[1] = 0;
}

// We are using the block distribution. We assume that n is large enough to
// assure that n/p is larger than sqrt(n). This means that we will always find the
// sieving prime in the first block, and so have to broadcast from the first
// processor to the others.
long sieving_prime;
int i;
bsp_push_reg( &sieving_prime,sizeof(long) );
bsp_sync();

for(i = 2; i * i < upperbound; i++) {
//Part 1: if first processor, get the newest sieving prime, broadcast. Search for newest prime starting from i.
if(s == 0){
int findPrimeNb;
for(findPrimeNb = i; findPrimeNb < blocksize; findPrimeNb++) {
if( blockvector[findPrimeNb] != 0) {
sieving_prime = blockvector[findPrimeNb];
//broadcast
int procNb;
for(procNb = 0; procNb < p; ++procNb){
bsp_put(procNb, &sieving_prime,&sieving_prime,0,sizeof(long));
}
break;
}
}
}
bsp_sync();

//Part 2: Sieve using the sieving prime
int sievingNb;
for(sievingNb = 0; sievingNb < blocksize; sievingNb++){
//check if element is multiple of sieving prime, if so, pcross out (put to zero)
if( blockvector[sievingNb] % sieving_prime == 0){
blockvector[sievingNb] = 0;
}
}

}

//part 3: get local primes to central area
int transferNb;
long transferPrime;
for(transferNb = 0; transferNb < blocksize; transferNb++){
transferPrime = blockvector[transferNb];
primes[transferPrime] = transferPrime;
}

// take the end time.
end_time = bsp_time();

//Print amount of taken time, only processor 0 has to do this.
if( s == 0 ){
printf("It took : %.6lf seconds for proc %d out of %d. \n", end_time-start_time, bsp_pid(), bsp_nprocs());
fflush(stdout);
}
bsp_pop_reg(&sieving_prime);
bsp_end();
}



int main(int argc, char **argv){

if(argc != 3) {
printf( "Usage: %s <proc count> <upper bound> <n", argv[ 0 ] );
exit(1);
}
//retrieve parameters
procs = atoi( argv[ 1 ] );
upperbound = atoi( argv[ 2 ] );

primes = (int *)malloc(upperbound*sizeof(int));

// init and call parallel part
bsp_init(bspSieve, argc, argv);
bspSieve();

//Print all non zeros of candidates, these are the primes.
// Primes only go to p*p <= n
int i;
for(i = 0; i < upperbound; i++) {
if(primes[i] > 0) {
printf("%d, ",primes[i]);
fflush(stdout);
}
}
return 0;
}

最佳答案

麻烦可能来自

blockvector[q] = q + s*blocksize;

只要blocksize在所有进程上都等于ceil(upperbound/p),就没有问题。由于 1 和 2 是 100 的约数,因此您的程序运行良好。

正如您在代码中所写的那样,情况并非总是如此......在调用 ./bspsieve 8 100 时,在最后一个过程中并非如此。 blockvector中有些值在100以上,在prime数组中写入时容易出现段错误。

纠正这种行为的方法是:

   blockvector[q] = q + s*ceil(upperbound/p);

(存储 ceil(...) 以更快地运行。)

在使用前将 prime 数组置零可能会更好。我没有检查它是否有效...试试吧!

再见,

弗朗西斯

关于c - Sieve Of Eratosthenes BSP C 实现没有找到所有素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20881956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com