Mentions légales du service

Skip to content
Snippets Groups Projects

Simgrid + MPI + threads

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Philippe SWARTVAGHER
    Edited
    test_simgrid.c 1.87 KiB
    /*
    To be built with:
    smpicc -I../../simgrid/build/ -I../../simgrid/ test_simgrid.c -L../../simgrid/build/lib/ -lsthread
    To be run with:
    LD_LIBRARY_PATH=/home/philippe/dev/simgrid/build/lib:$LD_LIBRARY_PATH smpirun -platform ~/dev/simgrid/examples/platforms/cluster_backbone.xml -np 2 ~/dev/memory-contention/test-simgrid/a.out
    */
    
    
    #include <mpi.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "sthread.h"
    
    
    #if 1
    #define pthread_create sthread_create
    #define pthread_join sthread_join
    #endif
    
    #define SIZE 409600
    
    
    static int rank;
    
    static void* ping_pong_thread_func(void* arg)
    {
    	int* buffer = malloc(SIZE*sizeof(int));
    	if (rank == 0)
    	{
    		buffer[SIZE-1] = 42;
    		fprintf(stderr, "Send posted\n");
    		MPI_Send(buffer, SIZE, MPI_INT, 1, 2, MPI_COMM_WORLD);
    		fprintf(stderr, "Send over\n");
    	}
    	else if (rank == 1)
    	{
    		fprintf(stderr, "Recv posted\n");
    		MPI_Recv(buffer, SIZE, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    		fprintf(stderr, "Recv over\n");
    		if (buffer[SIZE-1] != 42)
    		{
    			printf("Rank 1 didn't receive the good value ! (%d)\n", buffer[SIZE-1]);
    		}
    		else
    		{
    			fprintf(stderr, "OK\n");
    		}
    	}
    
    	free(buffer);
    
    	fprintf(stderr, "Going to join\n");
    	return NULL;
    }
    
    int main(int argc, char* argv[])
    {
    	int worldsize;
    
    	MPI_Init(&argc, &argv);
    
    	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    	MPI_Comm_size(MPI_COMM_WORLD, &worldsize);
    
    	if (worldsize != 2)
    	{
    		if (rank == 0)
    		{
    			fprintf(stderr, "We need (only) 2 processes\n");
    		}
    		MPI_Finalize();
    		return EXIT_FAILURE;
    	}
    
    	printf("Rank %d / %d\n", rank, worldsize);
    
    	pthread_t ping_pong_thread;
    	pthread_create(&ping_pong_thread, NULL, ping_pong_thread_func, NULL);
    	printf("[%d] Thread created\n", rank);
    	pthread_join(ping_pong_thread, NULL);
    	printf("[%d] Thread joined\n", rank);
    
    	fprintf(stderr, "Before barrier\n");
    	MPI_Barrier(MPI_COMM_WORLD);
    	fprintf(stderr, "After barrier\n");
    
    	MPI_Finalize();
    	return EXIT_SUCCESS;
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment