printf("processor %d got
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%c", valC[i]);
else
printf("%c, ", valC[i]);
}
printf("}\n");
}
MPI_Finalize();
return 0;
}
/**
ex: Se considera 3 procese, cu
rangurile 0, 1 si 2.
Procesele 1 si 2 detin fiecare
cate un vector de tip double, de
lungime N, care sunt trimise
rangului 0. Acesta scrie datele
intr-un fisier.
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
#define N 100
int getRank(){
int rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
return rank;
}
int getSize(){
int size;
MPI_Comm_size(MPI_COMM_WORLD,&size);
return size;
}
int main(int argc, char** argv){
MPI_Status status;
FILE* f;
f = fopen("ex2_Out.txt", "w");
MPI_Init(&argc,&argv);
//b)
if(getRank() == 1){
//proc 1 sent the array
int i;
double valD1[N] = {434.3, 32.12,
123.76};
//sends integers array
MPI_Send(&valD1,N,MPI_DOUBLE,0,1,MPI_COM
M_WORLD);
printf("processor %d sent
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%f", valD1[i]);
else
printf("%f, ", valD1[i]);
}
printf("}\n");
}
if(getRank() == 2){
//proc 2 sends the array
int i;
double valD2[N] = {32.323, 1.4,
14.23};
MPI_Send(&valD2,N,MPI_DOUBLE,0,1,MPI_COM
M_WORLD);
printf("processor %d sent
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%f", valD2[i]);
else
if(getRank() == 0){
//process 0 receive the the arrays
and writes them in file
int i;
double valD1[100];
double valD2[100];
//receive array of doubles
MPI_Recv(&valD1,100,MPI_DOUBLE,1,1,MPI_C
OMM_WORLD,&status);
fprintf(f,"processor %d writes
", getRank());
for(i = 0; i < 3; i++){
fprintf(f,"%f ",valD1[i]);
}
fprintf(f," from processor
1\n");
//receive array of doubles
MPI_Recv(&valD2,100,MPI_DOUBLE,2,1,MPI_C
OMM_WORLD,&status);
fprintf(f,"processor %d writes
", getRank());
for(i = 0; i < 3; i++){
fprintf(f,"%f ",valD2[i]);
}
fprintf(f," from processor
2\n");
MPI_Send(&valD,1,MPI_DOUBLE,1,2,MPI_COMM
_WORLD);
printf("processor %d sent
%f\n",getRank(),valD);
MPI_Send(&valC,1,MPI_CHAR,1,3,MPI_COMM_W
ORLD);
printf("processor %d sent
%c\n",getRank(),valC);
}
if(getRank() == 1){
//process 1 receive the values,
one by one
int valI;
double valD;
char valC;
MPI_Recv(&valI,1,MPI_INT,0,1,MPI_COMM_WO
RLD,&status);
printf("processor %d got
%d\n",getRank(),valI);
MPI_Recv(&valD,1,MPI_DOUBLE,0,2,MPI_COMM
_WORLD,&status);
printf("processor %d got
%f\n",getRank(),valD);
MPI_Recv(&valC,1,MPI_CHAR,0,3,MPI_COMM_W
ORLD,&status);
printf("processor %d got
%c\n",getRank(),valC);
}
//b)
}
fclose(f);
MPI_Finalize();
return 0;
}
/**
ex:Se considera doua procese,
cu rangurile 0, 1. Sa se scrie
un cod MPI in care:
- rangul 0 trimite o valoare
tip double, int, char, etc, catre rangul
1;
- rangul 0 trimite un vector de
tip double, int, char, etc, catre rangul
1;
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
else
printf("%c, ", valC[i]);
MPI_Send(&valI,1,MPI_INT,1,1,MPI_COMM_WO
RLD);
printf("processor %d sent
%d\n",getRank(),valI);
if(getRank() == 0){
//proc 0 sent the values, one by
}
printf("}\n");
}
if(getRank() == 1){
//process 1 receive the values,
one by one
int i;
int valI[100];
double valD[100];
char valC[100];
//receive array of integers
MPI_Recv(&valI,100,MPI_INT,0,1,MPI_COMM_
WORLD,&status);
printf("processor %d got
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%d", valI[i]);
else
printf("%d, ", valI[i]);
}
printf("}\n");
//receive array of doubles
MPI_Recv(&valD,100,MPI_DOUBLE,0,2,MPI_CO
MM_WORLD,&status);
printf("processor %d got
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%f", valD[i]);
else
printf("%f, ", valD[i]);
}
printf("}\n");
one
int i;
int valI[100] = {434, 32, 123};
double valD[100] = {32.323, 1.4,
14.23};
char valC[100] = {'c', 'a', 'v'};
//sends integers array
MPI_Send(&valI,100,MPI_INT,1,1,MPI_COMM_
WORLD);
printf("processor %d sent
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%d", valI[i]);
else
printf("%d, ", valI[i]);
MPI_Recv(&valC,100,MPI_CHAR,0,3,MPI_COMM
_WORLD,&status);
printf("processor %d got
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%c", valC[i]);
else
printf("%c, ", valC[i]);
}
printf("}\n");
}
MPI_Finalize();
return 0;
}
/**
int getRank(){
int rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
return rank;
}
int getSize(){
int size;
MPI_Comm_size(MPI_COMM_WORLD,&size);
return size;
}
int main(int argc, char** argv){
MPI_Status status;
MPI_Init(&argc,&argv);
//a)
}
printf("}\n");
//sends doubles array
MPI_Send(&valD,100,MPI_DOUBLE,1,2,MPI_CO
MM_WORLD);
printf("processor %d sent
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%f", valD[i]);
else
printf("%f, ", valD[i]);
}
printf("}\n");
//sends chars array
if(getRank() == 0){
//proc 0 sent the values, one by
one
int valI = 434;
double valD = 32.323;
char valC = 'c';
MPI_Send(&valC,100,MPI_CHAR,1,3,MPI_COMM
_WORLD);
printf("processor %d sent
{",getRank());
for(i = 0; i < 3; i++){
if(i == 2)
printf("%c", valC[i]);
int** mat;
int i,j;
mat = ( int** )malloc( N*sizeof(
int* ));
MPI_Init(&argc,&argv);
/* do something with array2 here
//initialize the matrix
for ( i = 0; i < N; i++ ){
*/
}
mat[i] = ( int*
)malloc(N*sizeof(int) );
for(j = 0; j < N; j++)
mat[i][j] = i+j+5;
}
int sum = 0;
//if the current procces is not 3 then
send one line
if(getRank() != 3){
//just the number of the line
int line;
line = getRank();
MPI_Send(&line,1,MPI_INT,3,1,MPI_COMM_WO
RLD);
printf("send from %d \n",
getRank());
}else{
//process 3 received the values
for the lines
int line;
//sumarize the elements
for(i = 0; i < N; i++){
MPI_Recv(&line,1,MPI_INT,i,1,MPI_COMM_WO
RLD,&status);
printf("received from %d \n",
i);
for(j = 0; j < N; j++){
sum = sum + mat[line][j];
}
}
}
MPI_Finalize();
if(sum != 0)
printf("sum : %d \n", sum);
return 0;
}
ex4.
/* distribute portions of array1 to
slaves. */
for(an_id = 1; an_id < num_procs;
an_id++) {
start_row =
an_id*num_rows_per_process;
ierr = MPI_Send(
&num_rows_to_send, 1, MPI_INT,
an_id, send_data_tag,
MPI_COMM_WORLD);
ierr = MPI_Send(
&array1[start_row],
num_rows_per_process,
MPI_FLOAT, an_id,
send_data_tag, MPI_COMM_WORLD);
}
/* and, then collect the results from
the slave processes,
* here in a variable called array2,
and do something with them. */
for(an_id = 1 an_id < num_procs;
an_id++) {
MPI_Reduce(&sum, &sumint, 1,
MPI_DOUBLE, MPI_SUM, 0, wcomm);
if(my_rank==0)
printf("Integrale cu
trapeze=%.7g\n\n",sumint);
MPI_Finalize();
return 0;
//compilare:
MPI_Comm_size(wcomm,&nproc);
//MPI_Comm_rank(wcomm,&my_rank);
}
ex9.
#include<stdlib.h>
#include<stdio.h>
#include<mpi.h>
#include<math.h>
#define N 100
double sinc(double x)
{
if(x==0.) return 1.0;
else return sin(x)/x;
}
int main(int argc, char **argv)
{
/****Inceput declaratii********/
int my_rank,nproc,source,dest;
int i;
double pi=4.0*atan(1);
double *buf,*rbuf;
MPI_Status status;
/********Sfarsit declaratii****/
/***********Init MPI***********/
MPI_Init(&argc,&argv);
MPI_Comm wcomm;
wcomm=MPI_COMM_WORLD;
MPI_Comm_size(wcomm,&nproc);
MPI_Comm_rank(wcomm,&my_rank);
/*********Sf MPI**************/
if((rbuf=(double
*)malloc(N*sizeof(double)))==NULL)
{
fprintf(stderr,"Nu pot aloca RAM
pt proc %d",my_rank);
MPI_Abort(wcomm,1);
}
if(my_rank==0)
{
if((buf=(double
*)malloc(N*sizeof(double)))==NULL)
{
fprintf(stderr,"Nu pot aloca
RAM pt proc %d",my_rank);
MPI_Abort(wcomm,1);
}
for(i=0;i<N;i++)
*(buf+i)=sinc(i*pi/N);
}
MPI_Scatter(buf,N,MPI_DOUBLE,rbuf,N,MPI_
DOUBLE,0,wcomm);
if(my_rank==2)
{
for(i=0;i<10;i++)
fprintf(stdout,"%.7g\t%.7g\t%.7
g\t%.7g\t%.7g\t%.7g\t%.7g\t%.7g\t%.7g\t%
.7g\t\n",*(rbuf+i),*(rbuf+i+1),
*(rbuf+i+2),*(rbuf+i+3),*(rbuf+
i+4),*(rbuf+i+5),*(rbuf+i+6),*(rbuf+i+7)
,*(rbuf+i+8),*(rbuf+i+9));
}
if(my_rank==0)
{
free(buf);
free(rbuf);
}
else free(rbuf);
MPI_Finalize();
return 0;
1. Sum(1/n!)
<mpi.h>
<stdio.h>
<stdlib.h>
MPI_Finalize()
int ping_pong_count = 0;
int partner_rank = (world_rank + 1) %
== ping_pong_count %
2) {
// Increment the ping pong count before you send it
ping_pong_count++;
MPI_Send(&ping_pong_count, 1, MPI_INT, partner_rank, 0,
MPI_Allgather
MPI_COMM_WORLD);
printf("%d sent and incremented ping_pong_count "
"%d to %d\n", world_rank, ping_pong_count,
partner_rank);
} else {
MPI_Recv(&ping_pong_count, 1, MPI_INT, partner_rank, 0,
MPI_COMM_WORLD,
printf("%d received
MPI_Scatter
MPI_Gather MPI_STATUS_IGNORE);
ping_pong_count %d from %d\n",
world_rank, ping_pong_count, partner_rank);
}
}
Similar to
,
takes elements
from each process and gathers them to the
root process. The elements are ordered by the
rank of the process from which they were
received. The function prototype for MPI_Gather is
identical to that of MPI_Scatter .
exit(1);
free(sub_avgs);
}
free(sub_rand_nums);
}
int num_elements_per_proc =
atoi(argv[1]);
// Seed the random number generator to
get different results each time
srand(time(NULL));
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
MPI_Init(NULL, NULL);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD,
&world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD,
&world_size);
// Create a random array of elements
on the root process. Its total
// size will be the number of elements
per process times the number
// of processes
float *rand_nums = NULL;
if (world_rank == 0) {
rand_nums =
create_rand_nums(num_elements_per_proc *
world_size);
}
// For each process, create a buffer
that will hold a subset of the entire
// array
float *sub_rand_nums = (float
*)malloc(sizeof(float) *
num_elements_per_proc);
assert(sub_rand_nums != NULL);
// Scatter the random numbers from the
root process to all processes in
// the MPI world
MPI_Scatter(rand_nums,
num_elements_per_proc, MPI_FLOAT,
sub_rand_nums,
num_elements_per_proc,
MPI_FLOAT, 0, MPI_COMM_WORLD);
// Compute the average of your subset
float sub_avg =
compute_avg(sub_rand_nums,
num_elements_per_proc);
// Gather all partial averages down to
the root process
float *sub_avgs = NULL;
if (world_rank == 0) {
sub_avgs = (float
*)malloc(sizeof(float) * world_size);
assert(sub_avgs != NULL);
}
MPI_Gather(&sub_avg, 1, MPI_FLOAT,
sub_avgs, 1, MPI_FLOAT, 0,
MPI_COMM_WORLD);
// Now that we have all of the partial
averages on the root, compute the
// total average of all numbers. Since
we are assuming each process computed
// an average across an equal amount
of elements, this computation will
// produce the correct answer.
if (world_rank == 0) {
float avg = compute_avg(sub_avgs,
world_size);
printf("Avg of all elements is
%f\n", avg);
// Compute the average across the
original data for comparison
float original_data_avg =
compute_avg(rand_nums,
num_elements_per_proc * world_size);
printf("Avg computed across original
data is %f\n", original_data_avg);
}
// Clean up
if (world_rank == 0) {
free(rand_nums);