-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparallelshuffle.c
More file actions
222 lines (195 loc) · 4.28 KB
/
parallelshuffle.c
File metadata and controls
222 lines (195 loc) · 4.28 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Author:Featherfruit on Github
Program to shuffle a r x c matrix
the shuffle is based on random permutation of
rows or columns.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include<sys/time.h>
//command line arguments start
int main(int argc, char *argv[])
{
if( argc != 6 )
{
printf("Invaild number of Arguments entered \n");
return -1;
}
int r, c;
//convert string to integer values
r = atoi(argv[1]);
c = atoi(argv[2]);
//function prototypes
srand( time(NULL) );
void shufflerc (int **,int,int,char,int,int);
void writeToFile(FILE*, int**, int, int);
//to calculate executiontime
struct timeval start, end;
//start of execution
gettimeofday(&start, NULL);
int **matrix = (int**)malloc(r*sizeof(int*));
for(int i = 0; i < r; i++)
matrix[i] = malloc(sizeof(int)*c);
//file handling starts
// declaring and opening the file containing input matrix (1)
FILE *fp = fopen(argv[3],"r");
// Handling edge cases
if (fp == NULL)
return 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
fscanf(fp, "%d", &matrix[i][j]);
}
fclose(fp);
fp = fopen(argv[4],"r");
// Handling edge cases
if (fp == NULL)
{
printf("fp was null case use a file with commands");
return 0;
}
char rcidentifier;
int i,j,noofcommands =1;
#pragma omp parallel
{
#pragma omp parallel for
for(int counter = 0; counter < noofcommands; counter++)
{
fscanf(fp,"%c %d %d\n",&rcidentifier,&i,&j);
shufflerc(matrix,r,c,rcidentifier,i,j);
}
}
fclose(fp);
//writing shuffled matrix to outputmatrix file handling(3)
FILE *outFPtr = fopen(argv[5], "w");
writeToFile(outFPtr,matrix, r, c);
//end of execution
gettimeofday(&end, NULL);
//free matrix to prevent memory leaks?
for (int i = 0; i < r; i++)
free(matrix[i]);
free(matrix);
//execution time calculation
float exec_time = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
exec_time /= 1000000;
printf("Time taken to execute: %fs\n\n", exec_time);
return 0;
}
//functions
//function(1)
void writeToFile(FILE *outFPtr, int **matrix, int r, int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
fprintf(outFPtr, "%d ", matrix[i][j]);
fprintf(outFPtr, "\n");
}
}
//function(2) - join the arrays then shuffle it put it back
void shufflerc(int **matrix,int r, int c, char rcidentifier,int i, int j)
{
#pragma omp parallel
{
if(rcidentifier =='R')
{ //merge(1)
int merge[2*c];
#pragma omp parallel for
for(int a=0;a<c;a++)
{
//i th row
merge[a]=matrix[i][a];
}
#pragma omp parallel for
for(int a=0;a<c;a++)
{
//j th row
merge[a+c]=matrix[j][a];
}
//shuffle(2)
// seed the random number generator with the current time to ensure random
//numbers generated are different each time our program runs
srand(time(NULL));
#pragma omp parallel for
for (int s = 0; s < 2*c; s++)
{
int swap_index = rand() % 2*c;
int temp = merge[s];
merge[s] = merge[swap_index];
merge[swap_index] = temp;
}
//split the merged array shuffle achieved(3)
#pragma omp parallel for
for(int a=0;a<c;a++)
{
//i th row
matrix[i][a]=merge[a];
}
#pragma omp parallel for
for(int a=0;a<c;a++)
{
//j th row
matrix[j][a]=merge[a+c];
}
}
else if(rcidentifier == 'C')
{ //merge(1)
int merge[2*r];
#pragma omp parallel for
for(int a=0;a<r;a++)
{
//i th row
merge[a]=matrix[a][i];
}
#pragma omp parallel for
for(int a=0;a<r;a++)
{
//j th row
merge[a+r]=matrix[a][j];
}
#pragma omp parallel for
for (int s = 0; s < 2*r; s++)
{
int swap_index = rand() % 2*r;
int temp = merge[s];
merge[s] = merge[swap_index];
merge[swap_index] = temp;
}
//shuffle(2)
srand(time(NULL));
#pragma omp parallel for
for (int s = 0; s < 2*r; s++)
{
int swap_index = rand() % 2*r;
int temp = merge[s];
merge[s] = merge[swap_index];
merge[swap_index] = temp;
}
//split the merged array shuffle achieved
#pragma omp parallel for
for(int a=0;a<r;a++)
{
//i th row
matrix[i][a]=merge[a];
}
#pragma omp parallel for
for(int a=0;a<r;a++)
{
//j th row
matrix[j][a]=merge[a+r];
}
}
}
}
/*
random comments
rccommands file will look like this
R 2 4 max will be R 9999 9999?
C 5 6
R 9 10
R 120 34
parllelize file handling?
*/