Skip to content

Commit 7765045

Browse files
committed
fixed samplesort
added samplesort reading from file
1 parent 53efda2 commit 7765045

File tree

3 files changed

+352
-25
lines changed

3 files changed

+352
-25
lines changed

makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ CFLAGS=-I.
99
%.o: %.cpp
1010
$(CXX) -c -o $@ $< $(CFLAGS)
1111

12-
all: samplesort seqsort
12+
all: samplesortdisk samplesort seqsort
1313

1414
seqsort: seqsort.o
1515
$(LINK) -o $@ $<
1616

1717
samplesort: samplesort.o
1818
$(LINK) -o $@ $<
19-
#data: data.o
20-
# $(LINK) -o $@ $<
19+
samplesortdisk: samplesortdisk.o
20+
$(LINK) -o $@ $<
2121

2222

2323
#select: select.o
2424
# $(LINK) -o $@ $<
2525

2626
clean:
27-
rm -f *.o *~ seqsort samplesort
27+
rm -f *.o *~ seqsort samplesort samplesortdisk

samplesort.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#define DEBUG_INITIAL_SORT 0
88
#define DEBUG_SPLITTERS 0
99
#define DEBUG_BUCKETS 0
10-
#define DEBUG_FINAL_SORT 1
11-
#define USE_SMALL_ARRAY 1
10+
#define DEBUG_FINAL_SORT 0
11+
#define USE_SMALL_ARRAY 0
1212

1313
char * ws;//strong or weak scaling indicator
1414
int rank,npes, root =0;
@@ -147,17 +147,17 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
147147

148148
#if DEBUG_BUCKETS
149149
for (i=0;i<count;i++) {
150-
printf("Element in rank:%d %d %lu\n",rank,i,a[i]);
150+
printf("rank=%d i=%d a[i]=%lu\n",rank,i,a[i]);
151151
}
152152
MPI_Barrier(comm);
153153
for (i=0;i<npes;i++){
154-
printf("Counts in rank:%d %lu %lu\n",rank,global_splitters[i],send_counts[i]);
154+
printf("rank=%d global_splitters[i]=%lu send_counts[i]=%lu\n",rank,global_splitters[i],send_counts[i]);
155155
}
156156
#endif
157157
recv_counts = (int *) malloc(npes*sizeof(int));
158158
//each processor now tells each other how many elements they will recieve
159159
//i.e. how many elements are in their bucket
160-
MPI_Alltoall(send_counts,1,MPI_LONG,recv_counts,1,MPI_LONG,comm);
160+
MPI_Alltoall(send_counts,1,MPI_INT,recv_counts,1,MPI_INT,comm);
161161
//imagine npes=3. Then
162162
//process 0: recv_counts[0]=send_counts[0] from p0,
163163
//recv_counts[1]=send_counts[0] from p1,recv_counts[2]=send_counts[0]
@@ -167,7 +167,7 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
167167
#if DEBUG_BUCKETS
168168
MPI_Barrier(comm);
169169
for (i=0;i<npes;i++){
170-
printf("%d %lu %lu\n",rank,send_counts[i],recv_counts[i]);
170+
printf("rank=%d send_counts=%lu recv_counts=%lu\n",rank,send_counts[i],recv_counts[i]);
171171
}
172172
#endif
173173
//The processors now have to send the proper elements to each other.
@@ -184,14 +184,10 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
184184

185185
recv_offsets[i] = j;
186186
j += recv_counts[i];
187-
printf("recv_counts %d %d\n",rank,recv_counts[i]);
188187
}
189188
//buckets have different sizes given by j
190-
printf("jj %d %d\n",rank,j);
191-
for (i =0;i<(npes-1);i++)
192-
printf("%d %d\n",rank,global_splitters[i]);
193189
buckets = (long int *)malloc (j*size);
194-
// MPI_Alltoallv(a,send_counts,send_offsets,MPI_LONG,buckets,recv_counts,recv_offsets,MPI_LONG,comm);
190+
MPI_Alltoallv(a,send_counts,send_offsets,MPI_LONG,buckets,recv_counts,recv_offsets,MPI_LONG,comm);
195191
//alltoallv: Imagine sends for p0, send_counts[0]=5,send_counts[1]=2,
196192
//send_counts[2]=7. Then a[0-4] from p0 goes to buckets[0-4] of p0
197193
//a[5-6] from p0 goes to buckets[0-1] of p1 and a[7-13] from p0 goes to
@@ -211,13 +207,23 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
211207
}
212208
MPI_Barrier(comm);
213209
for (i=0;i<j;i++) {
214-
printf("Rank:%d buckets[i]=%lu\n",rank,buckets[i]);
210+
printf("Rank:%d i=%lu buckets[i]=%lu\n",rank,i,buckets[i]);
215211
}
212+
MPI_Barrier(comm);
216213
#endif
217-
/* for (i=0;i<psize;i++) {
218-
printf("Rank after %d %lu\n",rank,a[i]);
219-
}*/
220-
214+
/*//Gather at the root the lengths of all the buckets
215+
MPI_Gather(&j,1,MPI_LONG,recv_counts,1,MPI_LONG,root,comm);
216+
//Compute index offsets where to put the bucket elements
217+
if (rank == root) {
218+
k = 0;
219+
for (i=0;i<count;i++) {
220+
recv_offsets[i]=k;
221+
k += recv_counts[i];
222+
}
223+
//gather sorted data
224+
*/
225+
//Now we have to put all the buckets together in the original array a
226+
221227
free(local_splitters);
222228
free(all_splitters);
223229
free(global_splitters);
@@ -226,7 +232,7 @@ void samplesort(long int *a,size_t count,size_t size,int (compare)(const void *,
226232
free(recv_counts);
227233
free(send_offsets);
228234
free(recv_offsets);
229-
free(buckets);
235+
//free(buckets);
230236
}
231237

232238
void sort(long int n)
@@ -258,9 +264,12 @@ void sort(long int n)
258264
a[i] = rand_interval(rank,rank*2+30);
259265
}
260266
#endif
267+
double t1,t2;
268+
t1 = MPI_Wtime();
261269
samplesort(a,psize,sizeof(long int),compare,MPI_COMM_WORLD);
262-
263-
free(a);
270+
t2 = MPI_Wtime();
271+
printf("Time elapsed in rank %d was %f\n",rank, t2-t1);
272+
//free(a);
264273
}
265274

266275
int main(int argc,char** argv)
@@ -303,5 +312,4 @@ int main(int argc,char** argv)
303312
//Finalize
304313
MPI_Finalize();
305314
return 0;
306-
}
307-
315+
}

0 commit comments

Comments
 (0)