Commit c1e5abb7 authored by Alexandru Dura's avatar Alexandru Dura

Ring communication

parent abd12736
CFLAGS = -O3 -march=native -Wall -std=c99
LDFLAGS = -lm
SOURCES = $(wildcard *.c)
TARGETS =
TARGETS := $(patsubst %.c,%.run,$(SOURCES)) $(TARGETS)
DEPS = $(wildcard *.h)
CC = mpicc
all : $(TARGETS)
time : $(patsubst %.run,%.time,$(TARGETS))
%.run : %.c $(DEPS)
$(CC) $(CFLAGS) -DSCHEDULE=static -o $@ $< $(LDFLAGS)
%.time : %.run
@echo $<
@time -f "\t%e real" ./$< > /dev/null
clean :
rm -f *.o
rm -f *.run
#include <mpi.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define DEFAULT_COMM MPI_COMM_WORLD
#define EAGER_PROTO 1
#if EAGER_PROTO
// using rendez-vous protocol
#define SEND MPI_Issend
#define RECV MPI_Recv
#else
// defaults
#define SEND MPI_Ssend
#define RECV MPI_Recv
#endif
int main(int argc, char **argv) {
MPI_Init(NULL, NULL);
int nsize, rank;
MPI_Comm_size(DEFAULT_COMM, &nsize);
MPI_Comm_rank(DEFAULT_COMM, &rank);
int upneighbour = (rank + 1) % nsize;
int dnneighbour = (rank + nsize - 1) % nsize;
int *startdata = malloc(nsize * sizeof(int));
for (int i = 0; i < nsize; ++i)
startdata[i] = rank;
int *dnsend = malloc(nsize * sizeof(int));
for (int i = 0; i < nsize; ++i)
dnsend[i] = startdata[i] * 1000;
int *upsend = malloc(nsize * sizeof(int));
for (int i = 0; i < nsize; ++i)
upsend[i] = startdata[i];
int *uprecv = malloc(nsize * sizeof(int));
int *dnrecv = malloc(nsize * sizeof(int));
for (int i = 0; i < nsize; ++i) {
MPI_Request request_up, request_dn;
MPI_Isend(upsend, nsize, MPI_INT, upneighbour, 0, DEFAULT_COMM, &request_up);
MPI_Isend(dnsend, nsize, MPI_INT, dnneighbour, 0, DEFAULT_COMM, &request_dn);
MPI_Recv(uprecv, nsize, MPI_INT, upneighbour, 0, DEFAULT_COMM, MPI_STATUS_IGNORE);
MPI_Recv(dnrecv, nsize, MPI_INT, dnneighbour, 0, DEFAULT_COMM, MPI_STATUS_IGNORE);
MPI_Wait(&request_up, MPI_STATUS_IGNORE);
MPI_Wait(&request_dn, MPI_STATUS_IGNORE);
memcpy(dnsend, uprecv, nsize * sizeof(int));
memcpy(upsend, dnrecv, nsize * sizeof(int));
}
for (int i = 0; i < nsize; ++i) {
MPI_Barrier(DEFAULT_COMM);
if (i == rank) {
printf("Process %d/%d\n", rank, nsize);
for (int i = 0; i < nsize; ++i) {
printf("%d ", uprecv[i]);
}
printf("\n");
for (int i = 0; i < nsize; ++i) {
printf("%d ", dnrecv[i]);
}
printf("\n");
}
}
MPI_Finalize();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment