Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- obraz w tablicy (wiersze nastepujace po sobie)
- !!wejscie: rozne obrazy rozne rozmiary (dane obrazu mozna generowac random)
- zadanie: rozmycie - uśrednienie wartości swojej i wartości swoich sąsiadów (+) w tablicy
- ?? wiele obrazow w jednej tablicy?
- in: iloscobr, w1, h1, w2, h2
- mozna zahardkodowac wielkosc obrazow
- moga byc rozne width, height
- do slave wysylamy info o rozmiarze obrazka i jego indeksie (mozna jako struct, jest jakas metoda)
- (mozna przez dynamic arr)
- #include <stdio.h>
- #include <mpi.h>
- #include <stdlib.h>
- #include <math.h>
- #define PRECISION 0.000001
- #define RANGESIZE 1
- #define DATA 0
- #define RESULT 1
- #define FINISH 2
- const int picturesCount = 5;
- const char image[] = { 10, 5,
- 2, 4,
- 3, 9,
- 11, 11,
- 1, 10 };
- //#define DEBUG
- //
- double f (double x)
- {
- return sin (x) * sin (x) / x;
- }
- double
- SimpleIntegration (double a, double b)
- {
- double i;
- double sum = 0;
- for (i = a; i < b; i += PRECISION)
- sum += f (i) * PRECISION;
- return sum;
- }
- int getStartOfPicture(int pictureNumber){
- int=0;
- for(i=0;i<pictureNumber;i++){
- arrSize += image[i*2] * image[i*2+1];
- }
- }
- int
- main (int argc, char **argv)
- {
- int myrank, proccount,i;
- long pictureSize;
- long arrSize = 0;
- for(i=0;i<picturesCount;i++){
- arrSize += image[i*2] * image[i*2+1];
- }
- printf("arrsize: %li", arrSize);
- int *inputArr = malloc (sizeof (int) * arrSize);
- for (i=0; i<arrSize; i++)
- {
- inputArr[i] = 0;
- }
- double a = 1, b = 100;
- double range[2];
- double result = 0, resulttemp;
- int sentcount = 0;
- MPI_Status status;
- // Initialize MPI
- MPI_Init (&argc, &argv);
- // find out my rank
- MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
- // find out the number of processes in MPI_COMM_WORLD
- MPI_Comm_size (MPI_COMM_WORLD, &proccount);
- if (proccount < 2)
- {
- printf ("Run with at least 2 processes");
- MPI_Finalize ();
- return -1;
- }
- if (((b - a) / RANGESIZE) < 2 * (proccount - 1))
- {
- printf ("More subranges needed");
- MPI_Finalize ();
- return -1;
- }
- // now the master will distribute the data and slave processes will perform computations
- if (myrank == 0)
- {
- range[0] = a;
- // first distribute some ranges to all slaves
- for (i = 1; i < proccount; i++)
- {
- //range[1] = range[0] + RANGESIZE;
- int element = i-1;
- int width = image[element];
- int height = image[element+1];
- int numberOfElements = width*height;
- int *arr = malloc (sizeof (int) * numberOfElements + 2);
- arr[0] = width;
- arr[1] = height;
- int startElement = getStartOfPicture(element);
- //przypisywanie wartosci
- for(int j = startElement; j<numberOfElements;j++){
- arr[j+2] = image[j];
- arr[j+3] = image[j+1];
- }
- // send it to process i
- MPI_Send (arr, numberOfElements+2, MPI_INT, i, DATA, MPI_COMM_WORLD);
- sentcount++;
- range[0] = range[1];
- }
- do
- {
- // distribute remaining subranges to the processes which have completed their parts
- MPI_Recv (&resulttemp, 1, MPI_DOUBLE, MPI_ANY_SOURCE, RESULT,
- MPI_COMM_WORLD, &status);
- result += resulttemp;
- #ifdef DEBUG
- printf ("\nMaster received result %f from process %d",
- resulttemp, status.MPI_SOURCE);
- fflush (stdout);
- #endif
- // check the sender and send some more data
- range[1] = range[0] + RANGESIZE;
- if (range[1] > b)
- range[1] = b;
- #ifdef DEBUG
- printf ("\nMaster sending range %f,%f to process %d",
- range[0], range[1], status.MPI_SOURCE);
- fflush (stdout);
- #endif
- MPI_Send (range, 2, MPI_DOUBLE, status.MPI_SOURCE, DATA,
- MPI_COMM_WORLD);
- range[0] = range[1];
- }
- while (range[1] < b);
- // now receive results from the processes
- for (i = 0; i < (proccount - 1); i++)
- {
- MPI_Recv (&resulttemp, 1, MPI_DOUBLE, MPI_ANY_SOURCE, RESULT,
- MPI_COMM_WORLD, &status);
- #ifdef DEBUG
- printf ("\nMaster received result %f from process %d",
- resulttemp, status.MPI_SOURCE);
- fflush (stdout);
- #endif
- result += resulttemp;
- }
- // shut down the slaves
- for (i = 1; i < proccount; i++)
- {
- MPI_Send (NULL, 0, MPI_DOUBLE, i, FINISH, MPI_COMM_WORLD);
- }
- // now display the result
- printf ("\nHi, I am process 0, the result is %f\n", result);
- }
- else
- { // slave
- // this is easy - just receive data and do the work
- do
- {
- MPI_Probe (0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- if (status.MPI_TAG == DATA)
- {
- MPI_Recv (range, 2, MPI_DOUBLE, 0, DATA, MPI_COMM_WORLD,
- &status);
- // compute my part
- resulttemp = SimpleIntegration (range[0], range[1]);
- // send the result back
- MPI_Send (&resulttemp, 1, MPI_DOUBLE, 0, RESULT,
- MPI_COMM_WORLD);
- }
- }
- while (status.MPI_TAG != FINISH);
- }
- // Shut down MPI
- MPI_Finalize ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement