-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpi_pqsort.c
138 lines (115 loc) · 4.15 KB
/
mpi_pqsort.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
int validate (int *output, int num_elements)
{
int i = 0;
assert (output != NULL);
for (i = 0; i < num_elements - 1; i++)
{
if (output[i] > output[i + 1])
{
printf ("************* NOT sorted *************\n");
printf("Element [%d] = %d is greater than [%d] = %d \n", i, output[i], i+1, output[i+1]);
return -1;
}
}
printf ("============= SORTED ===========\n");
return 0;
}
int main (int argc, char **argv)
{
int *input = NULL;
//int *output = NULL;
int num_elements;
int i = 0;
int numtasks, rank, rc;
unsigned int timeval = 0;
MPI_Status recv_status;
int elems_per_task;
int adjust;
int part_start;
rc = MPI_Init(&argc, &argv);
if(rc != MPI_SUCCESS) {
printf("Error starting MPI program. Terminating. \n");
MPI_Abort(MPI_COMM_WORLD, rc);
}
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Running with rank : %d\n", rank);
if (argc < 2){
printf ("Usage: ./pqsort <num of elements>\n");
}
if( argc == 3){
timeval = (unsigned int)atof(argv[3]);
}
num_elements = atoi (argv[1]);
printf("Running with rank [%d] and num_elements[%d]\n", rank, num_elements);
if (!(input = (int *) calloc (num_elements, sizeof (int)))){
printf ("Memory error\n");
exit (EXIT_FAILURE);
}
if(rank == 0){
if(timeval == 0){
timeval = time ( NULL );
}
printf("The seed is : %d\n", timeval);
srand(timeval);
for(i = 0; i < num_elements ;i++){
input[i] = i;
//input[i] = rand()%1000000;
}
elems_per_task = (num_elements/numtasks);
adjust = num_elements - elems_per_task*numtasks;
if(adjust > 0){
part_start = elems_per_task + 1;
adjust --;
}
else{
part_start = elems_per_task;
}
for(i = 1; i < numtasks; i++){
if(adjust > 0){
MPI_Send((input + part_start), elems_per_task + 1, MPI_INT, i, 0, MPI_COMM_WORLD);
part_start += (elems_per_task + 1);
adjust -- ;
}
else{
MPI_Send((input + part_start), elems_per_task, MPI_INT, i, 0, MPI_COMM_WORLD);
part_start += (elems_per_task);
}
}
}
else{
printf("My rank is : %d\n", rank);
elems_per_task = (num_elements/numtasks);
adjust = num_elements - elems_per_task*numtasks;
part_start = 0;
for(i=0; i < rank; i++){
if(adjust > 0){
part_start += (elems_per_task + 1);
adjust --;
}
else{
part_start += (elems_per_task);
}
}
if(adjust > 0){
MPI_Recv((input + part_start), elems_per_task+ 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &recv_status);
}
else{
MPI_Recv((input + part_start), elems_per_task, MPI_INT, 0, 0, MPI_COMM_WORLD, &recv_status);
}
for(i = part_start; i < part_start + elems_per_task; i ++){
printf("[%d] [%d] = [%d] \n", rank, i, input[i]);
}
}
MPI_Finalize();
/*
if(validate (output, num_elements) != 0)
return EXIT_FAILURE;
*/
return EXIT_SUCCESS;
}