06:40

Parallel Computing

HelloWord for Parallel Computing

/*
============================================================================
Name : ads.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello MPI World in C
============================================================================
*/
#include
#include
#include "mpi.h"

int main(int argc, char* argv[]){
int my_rank; /* rank of process */
int p; /* number of processes */
int source; /* rank of sender */
int dest; /* rank of receiver */
int tag=0; /* tag for messages */
char message[100]; /* storage for message */
MPI_Status status ; /* return status for receive */

/* start up MPI */

MPI_Init(&argc, &argv);

/* find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

/* find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);


if (my_rank !=0){
/* create message */
sprintf(message, "Hello MPI World from process %d!", my_rank);
dest = 0;
/* use strlen+1 so that '\0' get transmitted */
MPI_Send(message, strlen(message)+1, MPI_CHAR,
dest, tag, MPI_COMM_WORLD);
}
else{
printf("Hello MPI World From process 0: Num processes: %d\n",p);
for (source = 1; source < p; source++) {
MPI_Recv(message, 100, MPI_CHAR, source, tag,
MPI_COMM_WORLD, &status);
printf("%s\n",message);
}
}
/* shut down MPI */
MPI_Finalize();


return 0;
}

bu hazır geliyor Open MPI ile :) şuan labdayım yah

06:38

Parallel Computing

/*
============================================================================
Name : lab.c
Author :
Version :
Copyright : Your copyright notice
Description : Array in elemanlarını toplayan C kodu
============================================================================
*/
#include
#include
#include "mpi/mpi.h"

int main(int argc, char* argv[]){
int my_rank; /* rank of process */
int p; /* number of processes */
int i;
int allsum =0;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);

int vector[10];
int arr[500];
int arrr[500];
int as = 500 / p;

if (my_rank == 0){
for (i = 0; i < 500; i++)
arr[i] = i;
}

MPI_Scatter(arr, as, MPI_INTEGER, arrr, as, MPI_INTEGER, 0, MPI_COMM_WORLD);


int sum = 0;

for (i = 0; i < as; i++)
sum += arrr[i];

printf("Myrank: %d, sum: %d\n", my_rank, sum);

MPI_Reduce(&sum, &allsum, 1, MPI_INTEGER, MPI_SUM, 0, MPI_COMM_WORLD);

//MPI_Gather(&sum, 1, MPI_INTEGER, vector, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);



if (my_rank == 0)
{
printf("%d ", allsum);
}


MPI_Finalize();


return 0;
}