Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following is a corrected version of your code. The debugging tool I used was valgrind. Install it, compile bspsieve with <code>-g</code>, and use <code>valgrind 8 30 ./bspsieve</code> and you will be told info about any memory errors.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; #include &lt;mcbsp.h&gt; /* Compile using gcc -Wall -g -o bspsieve bspsieve.c lib/libmcbsp1.1.0.a -pthread -lrt -lm -Iinclude */ int procs; float upperbound; int *primes; //////////////// //////////////// BSPSIEVE //SPMD function void bspSieve () { bsp_begin (procs); float 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 &lt; 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 (&amp;sieving_prime, sizeof (long)); bsp_sync (); for (i = 2; i * i &lt; 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 &lt;= blocksize; findPrimeNb++) { if (blockvector[findPrimeNb] != 0) { sieving_prime = blockvector[findPrimeNb]; //broadcast int procNb; for (procNb = 0; procNb &lt; p; ++procNb) { bsp_put (procNb, &amp;sieving_prime, &amp;sieving_prime, 0, sizeof (long)); } break; } } } bsp_sync (); //Part 2: Sieve using the sieving prime int sievingNb; for (sievingNb = 0; sievingNb &lt; blocksize; sievingNb++) { //check if element is multiple of sieving prime, if so, pcross out (put to zero) if (blockvector[sievingNb] != sieving_prime) 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 &lt; 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_end (); } //////////////// //////////////// //////////////// //////////////// MAIN int main (int argc, char **argv) { if (argc != 3) { printf("Usage: %s &lt;processor count&gt; &lt;upper bound&gt;\n", argv[0]); exit(1); } printf ("processors to use: %s \n", argv[1]); printf ("upper bound: %s \n", argv[2]); //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. // DO Primes only go to p*p &lt;= n? The rest of the numbers seem to be prime int i; for (i = 0; i &lt; upperbound; i++) { if (primes[i] &gt; 0) { printf ("%d, ", primes[i]); } } printf("\n"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload