File: mpi_pthread.c

package info (click to toggle)
eztrace 2.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,012 kB
  • sloc: ansic: 37,703; sh: 1,246; cpp: 1,181; perl: 910; makefile: 738; fortran: 327; f90: 320; python: 124
file content (50 lines) | stat: -rw-r--r-- 913 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
#include <pthread.h>
#include <unistd.h>


void* thread_fun(void* arg)
{
	printf("Thread going to sleep\n");
	sleep(3);
	printf("Thread done sleeping\n");
}

int main(int argc, char* argv[])
{
	int rank, worldsize;

	int provided;
	MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &provided);

	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &worldsize);

	printf("Hello from rank %d ( / %d processes)\n", rank, worldsize);

	MPI_Barrier(MPI_COMM_WORLD);

	pthread_t thread;
	pthread_create(&thread, NULL, thread_fun, NULL);

	int v;

	if (rank == 0)
	{
		v = 23;
		MPI_Send(&v, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
	}
	else
	{
		MPI_Recv(&v, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
		printf("[%d] received %d (expected %d)\n", rank, v, 23);
	}

	pthread_join(thread, NULL);

	MPI_Finalize();

	return EXIT_SUCCESS;
}