glibc detected on multiple malloc
Code:
/*
*Sieve of Eratosthenes
*
* Programmed by Michael J. Quinn
*
* Last modification: 7 September 2001
*/
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
int main(int argc, char *argv[])
{
int count; /* Local prime count */
double elapsed_time; /* Parallel execution time */
int first; /* Index of first multiple */
int global_count; /* Global prime count */
int high_value; /* Highest value on this proc */
int i;
int id; /* Process ID number */
int index; /* Index of current prime */
int low_value; /* Lowest value on this proc */
char *marked; /* Portion of 2,...,'n' */
int n=(double)atoi(argv[1]); /* Sieving from 2, ..., 'n' */
int p; /* Number of processes */
int proc0_size; /* Size of proc 0's subarray */
int prime; /* Current prime */
int size; /* Elements in 'marked' */
char initMarked[(int)sqrt(n)];
int sqn;
int nop=1;
int *primes;
sqn = sqrt((double)atoi(argv[1]));
if(sqn%2==0)
sqn = sqn/2;
else
sqn = sqn/2+1;
for (i = 0; i < sqn; i++) initMarked[i] = 0;
prime = 2;
low_value = 2;
index = 0;
nop = 1;
do {
if ((2*prime-1) * (2*prime-1) > (2*low_value-1))
first = ((2*prime-1) * (2*prime-1) - (2*low_value-1))/2;
else {
if (!((2*low_value-1) % (2*prime-1))) first = 0;
else{
first = (2*prime-1) - ((2*low_value-1) % (2*prime-1));
first = (first%2==0)?first/2:(first+2*prime-1)/2;
}
}
for (i = first; i < sqn; i +=2*prime-1){ initMarked[i] = 1;}
while (initMarked[++index]);
prime = index + 2;
nop++;
} while ((2*prime-1) * (2*prime-1) <=n );
primes=(int*) malloc (size*sizeof(int));;
index = 0;
for(i=0;i<sqn;i++){
if(!initMarked[i]){
primes[index]=2*i+3;
index++;
}
}
//MPI_Bcast(&primes,nop,MPI_INT,0,MPI_COMM_WORLD);
printf("%d\n",nop);
//MPI_Init(&argc,&argv);
int rc = MPI_Init(&argc,&argv);
if (rc != MPI_SUCCESS) {
printf ("Error starting MPI program. Terminating.\n");
MPI_Abort(MPI_COMM_WORLD, rc);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Comm_rank(MPI_COMM_WORLD,&id);
MPI_Comm_size(MPI_COMM_WORLD,&p);
elapsed_time = -MPI_Wtime();
/* Start the timer */
if (argc != 2) {
if (!id) printf("Command line: %s <m>\n", argv[0]);
MPI_Finalize();
exit(1);
}
n = atoi(argv[1]);
if(n%2==0)
n=n/2;
else
n=n/2+1;
/* Figure out this process's share of the array, as
well as the integers represented by the first and
last array elements */
low_value = 2 + (id)*(n-1)/p;
high_value = 1 + (id+1)*(n-1)/p;
size = high_value - low_value + 1;
/* Bail out if all the primes used for sieving are
not all held by process 0 */
proc0_size = (n-1)/p;
if (2*(2 + proc0_size) < (int) sqrt((double) ((2.0)*n))) {
if (!id) printf("Too many processes\n");
MPI_Finalize();
exit(1);
}
/* Allocate this process's share of the array. */
marked = (char*) malloc (size*sizeof(char));
if (marked == NULL) {
printf ("Cannot allocate enough memory\n");
MPI_Finalize();
exit(1);
}
for (i = 0; i < size; i++) marked[i] = 0;
index = 0;
do {
prime = primes[index++];
//printf("prime = %d id = %d\n",prime,id);
if (prime * prime > (2*low_value-1))
first = (prime* prime - low_value)/2;
else {
if (!((2*low_value-1) % prime)) first = 0;
else{
first = prime - ((2*low_value-1) % prime);
first = ((first%2==0)?first/2:(first+prime)/2);
}
}
for (i = first; i < size; i += prime) marked[i] = 1;
} while (index < nop);
count = 0;
for (i = 0; i < size; i++)
if (!marked[i]) count++;
MPI_Reduce(&count, &global_count, 1, MPI_INT, MPI_SUM,
0, MPI_COMM_WORLD);
/* Stop the timer */
elapsed_time += MPI_Wtime();
/* Print the results */
if (!id) {
printf("There are %d primes less than or equal to %d\n",
global_count, n);
printf("SIEVE (%d) %10.6f\n", p, elapsed_time);
}
MPI_Finalize();
return 0;
}
I am getting glibc detected at second malloc usage? Is there any specific reason?