2

I start n POSIX threads on process #0 to listen to incoming calls from processes #0...#n. However, my code is not working, instead I get a segfault. I think the problem might be because of overlapping buffers. I am new to C++. Can you suggest a solution?

void *listener(void *arg) {
    ...
    int status_switch;    
    while (true) {                
        MPI_Recv(&status_switch, 1, MPI_INT, fd->id, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);        
        ...
    }
}

int main(int argc, char * argv[])
{
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);    

    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);            

    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    if (world_rank == root)
    {
        int nthreads = world_size;
        threads=(pthread_t *)malloc(nthreads*sizeof(threads));
        fd=(struct_t *)malloc(sizeof(struct_t)*nthreads);

        //Start listeners for each node on node0
        for (int i = 0; i < world_size; i++) {                        
            fd[i].id=i;  
            pthread_create(&threads[i], NULL, listener, (void *)(fd+i) );                    
        }                                       
    }    

    int status_switch;
    status_switch = 0;    
    MPI_Send(&status_switch, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);

    ...
}
sda
  • 143
  • 1
  • 10
  • 1) for C++, use new instead of malloc with type-cast -- this will avoid many pointer problems, 2) try compiling with debug (g++ -g), setup ulimit for core dump (ulimit -c unlimited), run your binary, get the core dump and then inspect the stacktrace of your coredump with gdb. – mariusm Jun 23 '15 at 13:43
  • just a thought: do you check the content of the variable provided? also, you seem to initialize MPI with world_size number of threads, but you have world_size+1 (current thread + world_size of started new ones). – mariusm Jun 23 '15 at 13:54

1 Answers1

1

I'm not familiar with MPI, but you seem to be allocating the wrong amount of space here:

threads = (pthread_t *) malloc(nthreads*sizeof(threads));

It is very likely that your code segfaults because of this.

If threads is a pthread_t *, you want to allocate nthreads*sizeof(*threads) - enough space to hold nthreads instances of pthread_t.

Also, you shouldn't cast the result of malloc.

Community
  • 1
  • 1
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • Thanks for this hint. Ive changed the `threads=(pthread_t *)malloc(nthreads*sizeof(*pthread_t))` but I still get setfault. – sda Jun 23 '15 at 13:49