-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodd_even_sort.c
67 lines (59 loc) · 1.56 KB
/
odd_even_sort.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "odd_even_sort.h"
// if process is in the edge of the array, it's neighbor should be a negative number
void odd_even_sort(int num_proc, int location, int left, int right, Orientation orientation,
void* my_data,
MPI_Datatype* data_type,
MPI_Comm* comm,
void (*min)(void*, void*),
void (*max)(void*, void*))
{
MPI_Status status;
// Default orientation is ASCENDING
void (*f1)(void*, void*) = min;
void (*f2)(void*, void*) = max;
if (orientation == DESCENDING)
{
f1 = max;
f2 = min;
}
Cuboid other_data;
int other_source;
void (*func)(void*, void*);
for (int i = 0; i < num_proc; i++)
{
if (location % 2 == 0) // even location
{
if (i % 2 == 0) // even iteration -> communicate with right side
{
other_source = right;
func = f1;
}
else // odd iteration -> communicate with left side
{
other_source = left;
func = f2;
}
}
else // odd location
{
if (i % 2 == 0)// even iteration -> communicate with left side
{
other_source = left;
func = f2;
}
else // odd iteration -> communicate with right side
{
other_source = right;
func = f1;
}
}
// if this process's location is first or last, it will not communicate during some of the iterations
if (other_source >= 0)
{
// MPI_Send(my_data, 1, data_type, other_source, 0, comm);
// MPI_Recv(other_data, 1, data_type, other_source, 0, comm, &status);
MPI_Sendrecv(my_data, 1, *data_type, other_source, 0, &other_data, 1, *data_type, other_source, 0, *comm, &status);
func(my_data, &other_data);
}
}
}