Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<stdlib.h>
- #include<cmath>
- #include "mpi.h"
- #define N 100
- typedef struct {
- double real, imag;
- } Complex;
- void MIN(Complex *in, Complex *inout, int *len, MPI_Datatype *dptr)
- {
- int i;
- for (i = 0; i < *len; i++) {
- if(sqrt(inout[i].real*inout[i].real + inout[i].imag*inout[i].imag) > sqrt(in[i].real * in[i].real + in[i].imag * in[i].imag)){
- inout[i] = in[i];
- }
- }
- }
- void MAX(Complex *in, Complex *inout, int *len, MPI_Datatype *dptr)
- {
- int i;
- for (i = 0; i < *len; i++) {
- if(sqrt(inout[i].real*inout[i].real + inout[i].imag*inout[i].imag) < sqrt(in[i].real * in[i].real + in[i].imag * in[i].imag)){
- inout[i] = in[i];
- }
- }
- }
- Complex localMax(Complex *locArr, int len, MPI_Datatype dtp){
- Complex locMAX = locArr[0];
- for(int i=1; i<len; i++){
- if(sqrt(locArr[i].real*locArr[i].real + locArr[i].imag*locArr[i].imag) > sqrt(locMAX.real * locMAX.real + locMAX.imag * locMAX.imag)){
- locMAX = locArr[i];
- }
- }
- return locMAX;
- }
- Complex localMin(Complex *locArr, int len, MPI_Datatype dtp){
- Complex locMIN = locArr[0];
- for(int i=1; i<len; i++){
- if(sqrt(locArr[i].real*locArr[i].real + locArr[i].imag*locArr[i].imag) < sqrt(locMIN.real * locMIN.real + locMIN.imag * locMIN.imag)){
- locMIN = locArr[i];
- }
- }
- return locMIN;
- }
- int main(int argc, char **argv )
- {
- int rank, size;
- double time;
- MPI_Op myMAX, myMIN;
- MPI_Datatype mytype;
- int blocklens[1];
- MPI_Aint displs[1];
- MPI_Datatype old_types[1];
- MPI_Init( &argc, &argv );
- MPI_Comm_rank(MPI_COMM_WORLD, &rank );
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- int num = N/size;
- if(N%size !=0){
- num++;
- }
- int len = num*size;
- Complex *com = new Complex[len];
- Complex max;
- Complex *buff = new Complex[num];
- Complex local_MAX, resultMAX, local_MIN, resultMIN;
- blocklens[0] = 2;
- displs[0] = 0;
- old_types[0] = MPI_DOUBLE;
- MPI_Type_create_struct(1, blocklens, displs, old_types, &mytype);
- MPI_Type_commit(&mytype);
- if(rank == 0)
- {
- for(int i =0; i<len; i++){
- if(i >= N){
- com[i].real = com[0].real;
- com[i].imag = com[0].imag;
- }
- else{
- com[i].real = (double)(rand()%1000) / (double)(rand()%1000 + 1);
- com[i].imag = (double)(rand()%1000) / (double)(rand()%1000 + 1);
- }
- }
- }
- time = MPI_Wtime();
- MPI_Scatter(com, num, mytype, buff, num, mytype, 0, MPI_COMM_WORLD);
- local_MAX = localMax(buff, num, mytype);
- local_MIN = localMin(buff, num, mytype);
- MPI_Op_create( (MPI_User_function *)MAX, true, &myMAX );
- MPI_Reduce(&local_MAX, &resultMAX, 1, mytype, myMAX, 0, MPI_COMM_WORLD);
- MPI_Op_free(&myMAX);
- MPI_Op_create( (MPI_User_function *)MIN, true, &myMIN );
- MPI_Reduce(&local_MIN, &resultMIN, 1, mytype, myMIN, 0, MPI_COMM_WORLD);
- MPI_Op_free(&myMIN);
- if(rank == 0){
- printf(" Time = %.8f\n", MPI_Wtime() - time);
- printf("N = %d\nNum processors = %d\n", N, size);
- printf("COM_MAX = %.4f + %.4f*i\n", resultMAX.real, resultMAX.imag);
- printf("COM_MIN = %.4f + %.4f*i\n", resultMIN.real, resultMIN.imag);
- }
- delete[] com;
- delete[] buff;
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement