-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitonicSort.cpp
More file actions
97 lines (87 loc) · 2.26 KB
/
bitonicSort.cpp
File metadata and controls
97 lines (87 loc) · 2.26 KB
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <mpi.h>
#include <unistd.h>
#include <algorithm>
#include <math.h>
int * getList(int size){ // Create a list of given size
int * list = new int[size];
for(int i = 0; i < size; i++){
list[i] = (i*7)%10;
}
return list;
}
int * printList(int * list, int size){
for(int i=0;i<size;++i){
std::cout<<list[i]<<" ";
}
std::cout<<std::endl;
return 0;
}
// Cube function we did in class
int cube(int i, int sendData){
int rank;
int size;
int dest;
int recvData;
int mask=1;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
mask = mask << i;
dest = rank ^ mask;
MPI_Send(&sendData,1,MPI_INT,dest,0,MPI_COMM_WORLD);
MPI_Recv(&recvData,1,MPI_INT,dest,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
if(rank < dest){
if(sendData<recvData) return sendData;
return recvData;
}
if(sendData<recvData) return recvData;
return sendData;
}
void printSorted(int data, int rank, int size){
int *dArray = new int [size];
MPI_Gather(&data,1,MPI_INT,dArray,1,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0){
std::cout << "Sorted List: " << std::endl;
printList(dArray, size);
}
return;
}
void bitonicSort(int size, int rank, int list_size){
int * list = new int[list_size];
int recv_data;
//Process 0 creates the list and sends it to processes
if(rank==0) {
list = getList(list_size);
std::cout << "Unsorted List: " << std::endl;
printList(list, list_size);
for(int i = 0; i < size; i++){
MPI_Send(&list[i],1,MPI_INT,i,0,MPI_COMM_WORLD);
}
}
// Each process recieves data and cubes it the number of times.
MPI_Recv(&recv_data,1,MPI_INT,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
//for(int j = log2(size)-1; j >= 0; --j){
for(int i = log2(size)-1; i >= 0; i--){
recv_data = cube(i, recv_data);
}
// for(int i = 0; i < log2(size)-1; ++i){
// recv_data = cube(i, recv_data);
// }
//}
MPI_Send(&recv_data,1,MPI_INT,0,0,MPI_COMM_WORLD);
printSorted(recv_data, rank, list_size);
}
int main(int argc, char** argv) {
int size;
int rank;
MPI_Init(&argc, &argv);
srand(time(NULL));
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//We can assume power of 2 processors
bitonicSort(size, rank, size);
MPI_Finalize();
return 0;
}