|
|
|
@ -2,8 +2,7 @@ |
|
|
|
|
|
|
|
|
|
#define INPUTFILE "zadanie1.in" |
|
|
|
|
|
|
|
|
|
int maximum1(int n, int **numbers); |
|
|
|
|
//int maximum2(int n, int (*numbers)[n]);
|
|
|
|
|
int maximum(int n, int **numbers); |
|
|
|
|
int *array_slice(int n, int **numbers, int start, int count); |
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
@ -38,39 +37,55 @@ int main(int argc, char *argv[]) { |
|
|
|
|
numbers = my_realloc(numbers, max * sizeof(int *)); |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the read number to the array in memory
|
|
|
|
|
numbers[count++] = number; |
|
|
|
|
// Store the read number to the array in memory, if in usable range
|
|
|
|
|
//if (number <= INT_MAX && number >= INT_MIN)
|
|
|
|
|
numbers[count++] = number; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int portion = count / slaves; |
|
|
|
|
int remainder = count % slaves; |
|
|
|
|
printf("count %d / slaves %d = %d mod %d \n", count, slaves, portion, remainder); |
|
|
|
|
|
|
|
|
|
//printf(" Sending numbers: ");
|
|
|
|
|
//for (i = 0; i < count; i++)
|
|
|
|
|
//printf("%d ", numbers[i]);
|
|
|
|
|
if (portion < 2) { |
|
|
|
|
printf("Please deacrease the number of processes.\n"); |
|
|
|
|
exit(EXIT_FAILURE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//printf("count %d / slaves %d = %d mod %d \n", count, slaves, portion, remainder);
|
|
|
|
|
|
|
|
|
|
// Send every slave the portion of the set
|
|
|
|
|
for (i = 1; i < processes; i++) {
|
|
|
|
|
int *slice = array_slice(count, &numbers, (i - 1) * portion, portion); |
|
|
|
|
MPI_Send(slice, portion, MPI_INT, i, 0, MPI_COMM_WORLD); |
|
|
|
|
for (i = 0; i < slaves; i++) {
|
|
|
|
|
int *slice = array_slice(count, &numbers, i * portion, portion); |
|
|
|
|
MPI_Send(slice, portion, MPI_INT, i + 1, 1, MPI_COMM_WORLD); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// The address where the modulo elements start
|
|
|
|
|
//int remainder = i * value + (count % processes);
|
|
|
|
|
//MPI_Send(numbers, count, MPI_INT, i, remainder, MPI_COMM_WORLD);
|
|
|
|
|
|
|
|
|
|
int *maximums = my_malloc((i + 1) * sizeof(int *)); |
|
|
|
|
// Receive all the maximums from the slaves
|
|
|
|
|
for (i = 1; i < processes; i++) {
|
|
|
|
|
MPI_Recv(&value, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &status); |
|
|
|
|
printf("Received %d from %d\n", value, i); |
|
|
|
|
for (i = 0; i < slaves; i++) {
|
|
|
|
|
MPI_Recv(&value, 1, MPI_INT, i + 1, 1, MPI_COMM_WORLD, &status); |
|
|
|
|
maximums[i] = value; |
|
|
|
|
//printf("i = %d, received %d from %d\n", i, value, i+1);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (remainder > 0) { |
|
|
|
|
int *slice = array_slice(count, &numbers, (i - 1) * portion, remainder); |
|
|
|
|
value = maximum1(remainder, &slice); |
|
|
|
|
printf("Calculated %d from remainder\n", value); |
|
|
|
|
if (remainder > 1) { |
|
|
|
|
int *slice = array_slice(count, &numbers, i * portion, remainder); |
|
|
|
|
maximums[i] = maximum(remainder, &slice); |
|
|
|
|
//printf("i = %d, max of remainders is %d\n", i, maximums[i]);
|
|
|
|
|
} |
|
|
|
|
else if (remainder == 1) { |
|
|
|
|
maximums[i] = numbers[count - 1]; |
|
|
|
|
//printf("i = %d, only remainder is %d\n", i, maximums[i]);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
printf("Maximum of numbers"); |
|
|
|
|
for (n = 0; n < count; n++) |
|
|
|
|
printf(" %d", numbers[n]); |
|
|
|
|
printf(" is %d\n", maximum(i + 1, &maximums)); |
|
|
|
|
|
|
|
|
|
//printf("i: %d, count: %d, max: %d\n", i, COUNT(maximums), maximum(i + 1, &maximums));
|
|
|
|
|
//printf("numbers: ");
|
|
|
|
|
//for (n = 0; n <= i; n++)
|
|
|
|
|
//printf("%d ", maximums[n]);
|
|
|
|
|
//printf("\n");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// This is a slave process
|
|
|
|
@ -78,44 +93,33 @@ int main(int argc, char *argv[]) { |
|
|
|
|
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); |
|
|
|
|
MPI_Get_count(&status, MPI_INT, &n); |
|
|
|
|
|
|
|
|
|
is_mpi_defined(n); |
|
|
|
|
//int numbers[n];
|
|
|
|
|
int *numbers = my_malloc(n * sizeof(int *)); |
|
|
|
|
// There is no way, I could find, to use dynamically allocated array...the pointer of numbers
|
|
|
|
|
// array changes after calling MPI_Recv...only fixed array worked here, otherwise exactly two
|
|
|
|
|
// elements are received all the time...meh
|
|
|
|
|
|
|
|
|
|
printf("address before: %p\n", numbers); |
|
|
|
|
MPI_Recv(numbers, n, MPI_INT, 0, 0, MPI_COMM_WORLD, &status); |
|
|
|
|
printf("address after: %p\n", numbers); |
|
|
|
|
//int *numbers = my_malloc(n * sizeof(int *));
|
|
|
|
|
//for (i = 0; i < n; i++)
|
|
|
|
|
//numbers[i] = numberss[i];
|
|
|
|
|
value = maximum1(n, &numbers); |
|
|
|
|
MPI_Send(&value, 1, MPI_INT, 0, rank, MPI_COMM_WORLD); |
|
|
|
|
if (n != MPI_UNDEFINED && n >= 2) { |
|
|
|
|
int *numbers = my_malloc(n * sizeof(int *)); |
|
|
|
|
|
|
|
|
|
//printf("address before: %p\n", numbers);
|
|
|
|
|
MPI_Recv(numbers, n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); |
|
|
|
|
//printf("address after: %p\n", numbers);
|
|
|
|
|
value = maximum(n, &numbers); |
|
|
|
|
MPI_Send(&value, 1, MPI_INT, 0, 1, MPI_COMM_WORLD); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
MPI_Finalize(); |
|
|
|
|
return (0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#define MAXIMUM { \ |
|
|
|
|
int value = 0; \
|
|
|
|
|
for (int i = 0; i < n; i++) { \
|
|
|
|
|
if (value < (*numbers)[i]) {\
|
|
|
|
|
value = (*numbers)[i]; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
return value; \
|
|
|
|
|
int maximum(int n, int **numbers) { |
|
|
|
|
int value = 0;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
if (value < (*numbers)[i]) { |
|
|
|
|
value = (*numbers)[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int maximum1(int n, int **numbers) MAXIMUM; |
|
|
|
|
|
|
|
|
|
//int maximum2(int n, int (*numbers)[n]) MAXIMUM;
|
|
|
|
|
|
|
|
|
|
int *array_slice(int n, int **numbers, int start, int count) { |
|
|
|
|
int *slice = my_malloc(count * sizeof(int *)); |
|
|
|
|
// Make sure that iteration won't try to go outside of numbers array
|
|
|
|
|
for (int i = 0; i < count && i < n; i++)
|
|
|
|
|
slice[i] = (*numbers)[start + i];
|
|
|
|
|
|
|
|
|
|