-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbp.c
More file actions
86 lines (77 loc) · 1.91 KB
/
bp.c
File metadata and controls
86 lines (77 loc) · 1.91 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
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int split)
{
int i, j;
for (i = 0; i < split-1; i++)
// Last i elements are already in place
for (j = 0; j < split-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void main (int argc, char **argv){
int numranks, rank, n, size;
int *data = NULL;
int *sub;
// initialize MPI
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numranks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank == 0){
if(argc < 2){
printf("Please give the number of elements you would like to sort and try again.\n");
MPI_Finalize();
}
n = atoi(argv[1]);
if(n%numranks != 0){
size = n/numranks+1;
}
else{
size = n/numranks;
}
data = (int *)malloc(numranks * size * sizeof(int));
//Randomly assign values to array
for(int i = 0; i < n; i++){
data[i] = rand() %n;
}
}
MPI_Barrier(MPI_COMM_WORLD);
double startTime = MPI_Wtime();
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if(n%numranks !=0){
size = n/numranks+1;
}
else{
size = n/numranks;
}
//scatter data
sub = (int *)malloc(size * sizeof(int));
MPI_Scatter(data, size, MPI_INT, sub, size, MPI_INT, 0, MPI_COMM_WORLD);
bubbleSort(sub,size);
//printf("Rank: %d, sub: \n",rank);
//printArray(sub,size);
MPI_Gather(sub, size, MPI_INT, data, size, MPI_INT, 0, MPI_COMM_WORLD);
if(rank == 0){
bubbleSort(data, size*numranks);
double endTime = MPI_Wtime();
printf("Numranks: %d, N: %d, Time: %f\n",numranks, n, endTime-startTime);
}
free(data);
MPI_Finalize();
}