gpt4 book ai didi

c - BSP 实现 Sieve Of Eratosthenes 不打印 C

转载 作者:太空宇宙 更新时间:2023-11-04 02:52:24 25 4
gpt4 key购买 nike

我正在使用 BSP 在 C 中实现埃拉斯托梯尼筛分法的并行算法。

我的代码编译并执行但不打印素数。例如,当我执行 ./bspsieve 2 1000 时,我得到的唯一输出是“它花了:0.000371 秒 proc 0 out of 2.”虽然它应该打印所有找到的素数!奇怪的是,该算法看起来确实有效。如果我在上面的例子中使用更大的上限,它需要更长的时间。当我分配更多的处理器时,它花费的时间更少。所以可能在某个地方犯了一个愚蠢的错误,但我在使用 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*i <= upperbound; i++) {
if(primes[i] > 0) {
printf("%d, ",primes[i]);
}
}
return 0;
}

最佳答案

printf 不会自动包含尾随换行符,并且它通常不会在输出换行符之前刷新输出缓冲区;所以可能你只需要添加一个

printf("\n");

在您的程序结束时,就在您的return 0; 之前。

或者,或者另外,如果您想随时查看输出(如果 BSP 允许的话),您可以添加一个

fflush(stdout);

printf("%d, ",primes[i]); 之后,显式刷新输出缓冲区。

关于c - BSP 实现 Sieve Of Eratosthenes 不打印 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828955/

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