thread safe malloc?

Zaheerul Arefeen <[email protected]> Mon, 12 Apr 2004 18:42:30 -0700 (PDT)
Newsgroups gmane.network.myrinet.general
Message-ID <[email protected]>
Hi
I'm using mpich-1.2.5..12 on top of gm 1.6.5 on a
linux cluster.
Please see my attached code, where i created multiple
pthreads from each mpi processes and used malloc and
free iteratively in the thread body. at the end,
threads are synchronized with pthread_join. threre is
no communication among the processes. but two weird
thing happens when i run this code.

1. sometimes the main exit without completing the
thread body or main body (possibly some processes
crush!!)

2. sometimes some of the processes complete execution
and other waits for thread join (i guess).

But the very same code runs successfully on Sun mpi in
a shared memory machine. 

I wonder the new mpich-gm 1.2.5..12 does provide
thread safe malloc!! or i'm not getting this because
its on top of gm-1 rather than gm-2. Is there any
mpich-gm log files where i can find where my code is
actually executed(may be not on all machines of my
machine file) and whether or not my processes are
being crused at some nodes.

I appreciate your support in this regard.
thanks.
Arefeen
University of Windsor

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com

_______________________________________________
Myrinet mailing list
[email protected]
http://email.osc.edu/mailman/listinfo/myrinet
mpit3.c (text/plain, 2.4 KB)
/*****************************************************************************
* FILE: join1.c
* DESCRIPTION:
*   This example demonstrates how to "wait" for thread completions by using
*   the Pthread join routine.  Since not all current implementations of
*   Pthreads create threads in a joinable state, the threads in this
*   example are explicitly created in a joinable state so that they can
*   be joined later.
*
* SOURCE: 8/98 Blaise Barney
* LAST REVISED:  10/8/99 Blaise Barney
******************************************************************************/
#include <pthread.h>
#include <mpi.h>
#include <stdio.h>
#include <malloc.h>

#define NUM_THREADS	10

void *BusyWork(void *arg)
{
   int i,j,myid,mythread;
   double result=0.0;
   double * x;

   MPI_Comm_rank (MPI_COMM_WORLD, &myid);
   mythread=(int)arg;



   for (i=0; i<10000; i++)
   {


     x = (double*) malloc (10000*sizeof(double));
	 for (j = 0; j < 10000; j++)
	     x[j] = 2.3 * j / 20.5;
     free(x);
     //printf("%d\n", i);

   }


   printf("Done from [P%dT%d]\n",myid,mythread);
   pthread_exit((void *) 0);
   //pthread_exit(NULL);
}

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


   int total_process, process_id;
   pthread_t thread[NUM_THREADS];
   pthread_attr_t attr;
   int rc, t, status,prov;
   MPI_Status stat;


   MPI_Init_thread(&argc,&argv,MPI_THREAD_MULTIPLE, &prov);
   MPI_Comm_size(MPI_COMM_WORLD, &total_process);
   MPI_Comm_rank(MPI_COMM_WORLD, &process_id);

	MPI_Barrier(MPI_COMM_WORLD);

   /* Initialize and set thread detached attribute */
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   printf("provided thread=%d\n",prov);

   for(t=0;t<NUM_THREADS;t++)
   {
      printf("Creating process %d thread %d\n",process_id, t);
      rc = pthread_create(&thread[t], &attr, BusyWork, (void*) t);
      if (rc)
      {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Free attribute and wait for the other threads */
   pthread_attr_destroy(&attr);

   for(t=0;t<NUM_THREADS;t++)
   {
      printf("P%dT%d\n",process_id,t);
      rc = pthread_join(thread[t], (void **)&status);
      if (rc)
      {
         printf("ERROR return code from pthread_join() is %d\n", rc);
         exit(-1);
      }
      printf("Completed join with process %d thread %d status= %d\n",process_id, t, status);
   }
	MPI_Barrier(MPI_COMM_WORLD);

	printf("done\n");

   MPI_Finalize();
   pthread_exit(NULL);
}