-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw8.c
208 lines (167 loc) · 5.21 KB
/
hw8.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
============================================================================
Name : hw8.c
Paz Lavi 208944124
Daniel Kozachkevich 203690359
============================================================================
*/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#include "hw8.h"
#include "cuda.h"
int main(int argc, char *argv[]){
int numOfProcs, myRank, arr_size = 0;
MPI_Status status;
int *merged_array;
int *received_array;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &numOfProcs);
if (numOfProcs != MAX_PROC) {
printf("Program requires %d nodes\n", MAX_PROC);
MPI_Finalize();
exit(1);
}
if (myRank == MASTER) {
if (argc != 2) {
printf(argc < 2 ? "Data file is required!" : //handle input error
"Only data file is required!");
MPI_Abort(MPI_COMM_WORLD, 1);
}
int *initial_array;
//space allocation and read the data from the file
initArrays(argv[1], &initial_array, &received_array, &arr_size);
//send size of array to slave
MPI_Send(&arr_size, 1, MPI_INT, SLAVE, 0, MPI_COMM_WORLD);
// send cuda task to slave
MPI_Send(initial_array + arr_size / 2, ((arr_size / 2)+ ((arr_size % 2) == 0 ? 0:1)), MPI_INT, SLAVE, 0,
MPI_COMM_WORLD);
//perform openmp task, do merge
merged_array = OpenMPTask(initial_array, arr_size);
//receive result from slave
MPI_Recv(received_array, arr_size, MPI_INT, SLAVE, 0, MPI_COMM_WORLD,
&status);
//merge both processes results
OpenMPFinalMergeTask(merged_array, received_array, RANGE_SIZE);
// Print array after merge
PrintHistogram(merged_array);
// free memory
free(initial_array);
} else {
//receive size of array from master
MPI_Recv(&arr_size, 1, MPI_INT, MASTER, 0, MPI_COMM_WORLD, &status);
//space allocation for the receive data
myIntArrCalloc(&received_array, ((arr_size / 2) + ((arr_size % 2) == 0 ? 0:1)));
// receive cuda task
MPI_Recv(received_array, ((arr_size / 2)+ ((arr_size % 2) == 0 ? 0:1)), MPI_INT, MASTER, 0,
MPI_COMM_WORLD, &status);
// perform cuda task
merged_array = CUDATask(received_array, ((arr_size / 2)+ ((arr_size % 2) == 0 ? 0:1)), arr_size);
//send result to master
MPI_Send(merged_array, arr_size, MPI_INT, MASTER, 0, MPI_COMM_WORLD);
}
// free memory
free(merged_array);
free(received_array);
MPI_Finalize();
return 0;
}
//space allocation and read the data from the file.
void initArrays(char *filename, int **initial_array, int **received_array,
int *size) {
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "Could not open file: %s\n", filename);
MPI_Abort(MPI_COMM_WORLD, 1);
}
int i;
if (fscanf(file, "%d", size) == 1) {
myIntArrCalloc(initial_array, *size); //space allocation set all to 0
myIntArrCalloc(received_array, *size); //space allocation set all to 0
for (i = 0; i < *size; i++) {
if (fscanf(file, "%d", (*initial_array) + i) != 1) { //read each line and insert to the array
fprintf(stderr, "row %d is not represented correctly!",
__LINE__);
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
}
fclose(file);
}
//perform calloc (set all to 0). if allocation failed the program abort
void myIntArrCalloc(int **arr, int size) {
*arr = (int*) calloc(size, sizeof(int)); // space allocation
if (arr == NULL) { //check if allocation succeeded. if not the program abort
fprintf(stderr, "Could not allocate array");
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
int* OpenMPTask(int *src_arr, int size) {
int *dst_arr;
myIntArrCalloc(&dst_arr, RANGE_SIZE); //space allocation
int *tmp_hist;
#pragma omp parallel //num_threads(size/2)
{
const int tid = omp_get_thread_num();
const int nthreads = omp_get_num_threads();
#pragma omp single
{
myIntArrCalloc(&tmp_hist, size * nthreads); //space allocation
}
#pragma omp for
for (int i = 0; i < size / 2; i++) // for openmp task we running only on first arr part
tmp_hist[tid * size + src_arr[i]]++;
// merge
#pragma omp for
for (int i = 0; i < RANGE_SIZE; i++)
for (int j = 0; j < nthreads; j++)
dst_arr[i] += tmp_hist[j * size + i]; // each thread merges specific cell in tmp_arr to dst_arr
}
free(tmp_hist);
return dst_arr;
}
int* CUDATask(int *arr, int size, int arr_size) {
return calculateHistogramm(arr, size, arr_size);
}
void OpenMPFinalMergeTask(int *dest_array, int *src_array, int size) {
#pragma omp for
for (int i = 0; i < RANGE_SIZE; i++)
dest_array[i] += src_array[i]; // each thread merges specific cell in tmp_arr to dst_arr
}
void PrintHistogram(int *arr) {
int i, sum = 0;
printHeadline(1);
//res = calcHistograma(arr, size);
for (i = 0; i < RANGE_SIZE; i++) {
if (i % 10 == 0)
printf("\n");
printf("| arr[%-3d] = %-3d |", i, arr[i]);
sum += arr[i];
}
printf("\n\n\nsum of array= %d", sum);
printRes(arr, RANGE_SIZE);
fflush(stdout);
}
void printRes(int *res, int size) {
int i, j;
printHeadline(2);
for (i = 0; i < size; i++) {
printf("%3d |", i);
for (j = 0; j < res[i]; j++) {
printf("*");
}
printf("\n");
}
printf("%s\n\n", LINE);
}
void printHeadline(int msg_num) {
if (!(msg_num == 1 || msg_num == 2))
return;
printf("\n%s", LINE);
printf("%s", LINE);
printf("%s", msg_num == 1 ? MSG1 : MSG2);
printf("%s", LINE);
printf("%s\n\n", LINE);
}