Note that there are some explanatory texts on larger screens.

plurals
  1. POMPI, calloc and free:
    primarykey
    data
    text
    <p>I'm trying to learn C and MPI, this program calculates the sum of n floats. But I have an error:</p> <p>/home/xx/PRIMO/primo.exe: free(): invalid next size (fast): 0x000000000109bda0 /home/xx/PRIMO/primo.exe: free(): invalid next size (fast): 0x00000000024fada0 </p> <p>Are 2 days that I don’t know which way to turn. Here the Program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;math.h&gt; #include "mpi.h" #define HOST 0 // prototypes void initialize( int argc, char *argv[] ); float *create_array( int n ); void data_scatter( float *numbers, float *numbers_loc, int packet_size, int rest ); void validations(); // global vars int menum, nproc, namelen; int main(int argc, char *argv[]) { initialize( argc, argv ); if( menum == HOST ) printf("Program started\n"); if( menum == HOST ) validations(); // ****** Declaring variebles ****** int n, tag, packet_size, rest, j, i; float *numbers, *numbers_loc; float sum = 0.0; numbers_loc = (float*) calloc( packet_size, sizeof (float) ); MPI_Status info; // ****** data distribution ****** scanf("%d", &amp;n); if( menum == HOST ) numbers = create_array( n ); MPI_Bcast( &amp;n, 1, MPI_INT, 0, MPI_COMM_WORLD ); double start_time = MPI_Wtime(); packet_size = n / nproc; rest = n % nproc; if( menum &lt; rest ) ++packet_size; data_scatter( numbers, numbers_loc, packet_size, rest ); // ****** partial sums, first level ****** for( j = 0; j &lt; packet_size; j++) { sum += numbers_loc[j]; } free(numbers_loc); // ****** iterating phase ****** for( i = 0; i &lt; log2(nproc); i++) { int pow1 = (int) pow(2, i); tag = pow1 + 15; if( (menum % pow1) == 0) { int pow2 = (int) pow(2,i+1); if( (menum % pow2) == 0 ) { float other_sum = 0; MPI_Recv( &amp;other_sum, 1, MPI_INT, menum + pow1, tag, MPI_COMM_WORLD, &amp;info ); sum += other_sum; } else { MPI_Send( &amp;sum, 1, MPI_INT, menum - pow1, tag, MPI_COMM_WORLD ); } } } // ****** printing result ****** if( menum == HOST ) { printf( "TOTAL SUM: %.2f\n", sum); } double end_time = MPI_Wtime(); double time = end_time - start_time; double max = 0; printf( "%d time %f\n", menum, time ); MPI_Reduce( &amp;time, &amp;max, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD ); if(menum == HOST) printf( "%d max time %f\n", menum, max ); printf("Program terminated\n"); MPI_Finalize(); return 0; } void initialize( int argc, char *argv[] ) { MPI_Init(&amp;argc,&amp;argv); MPI_Comm_rank(MPI_COMM_WORLD,&amp;menum); MPI_Comm_size(MPI_COMM_WORLD,&amp;nproc); } float *create_array( int n ) { float sequenzial_sum = 0; int i; printf("Creating Array of dimension: %d\n", n); float *array = (float*) calloc( n, sizeof(float)); srand( time(NULL) ); for( i=0; i &lt; n; i++) { array[i] = ((float) rand())/ ((float) RAND_MAX); sequenzial_sum += array[i]; printf("-%f-", array[i]); } printf("\n"); printf("Array creation terminated, sum = %.2f \n", sequenzial_sum); return array; } void data_scatter( float *numbers, float *numbers_loc, int packet_size, int rest ) { MPI_Status info; int start = 0, offset = packet_size, i, tag; if( menum == HOST ) { memcpy(numbers_loc, numbers, packet_size * sizeof(float)); for( i=1; i &lt; nproc; i++ ) { start += offset; if( rest == i) --offset; tag = 22 + i; MPI_Send( &amp;numbers[start], offset, MPI_INT, i, tag, MPI_COMM_WORLD ); } free(numbers); } else { tag = 22 + menum; MPI_Recv( numbers_loc, packet_size, MPI_INT, 0, tag, MPI_COMM_WORLD, &amp;info ); } } void validations() { if( ( fmod(log2( nproc ), 1.0) != 0 )) { printf("CPU number must ne a power of 2\n"); MPI_Abort( MPI_COMM_WORLD, -1 ); } } </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.
 

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