Commit abd12736 authored by Alexandru Dura's avatar Alexandru Dura

Exercise 6, part 1

parent db9a10e4
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
#ifndef _COMMON_H
#define _COMMON_H
#include <time.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#ifdef _OPENMP
#define TIME_REGION(T, X) \
do { \
double __start_time, __end_time; \
__start_time = omp_get_wtime(); \
X; \
__end_time = omp_get_wtime(); \
T = __end_time - __start_time; \
} while(0);
#else
#define TIME_REGION(T, X) \
do { \
time_t __start_time, __end_time; \
time(&__start_time); \
X; \
time(&__end_time); \
T = difftime(__end_time, __start_time); \
} while(0);
#endif
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#endif
#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_Ssend
#define RECV MPI_Recv
#else
// defaults
#define SEND MPI_Send
#define RECV MPI_Recv
#endif
double computeSum(uint64_t start, uint64_t end) {
double acc = 0.0;
for (uint64_t i = start; i < end; ++i) {
acc += 1 / ((double)(i) * (double)(i));
}
return acc;
}
int main(int argc, char **argv) {
int com_size, rank;
if (argc < 2) {
fprintf(stderr, "Insuficient arguments. Abort.\n");
return 1;
}
uint64_t size = atol(argv[1]);
double startTime, endTime;
MPI_Init(NULL, NULL);
startTime = MPI_Wtime();
MPI_Comm_size(DEFAULT_COMM, &com_size);
MPI_Comm_rank(DEFAULT_COMM, &rank);
// size for each process
uint64_t chunk_size = size / com_size;
uint64_t start = rank * chunk_size + 1;
uint64_t stop = start + chunk_size + 1;
double sum = computeSum(start, stop);
if (rank == 0) {
for (int i = 1; i < com_size; ++i) {
double tmp_sum;
MPI_Status status;
RECV(&tmp_sum, 1, MPI_DOUBLE, i, 0, DEFAULT_COMM, &status);
sum += tmp_sum;
}
} else {
SEND(&sum, 1, MPI_DOUBLE, 0, 0, DEFAULT_COMM);
}
endTime = MPI_Wtime();
if (rank == 0) {
printf("%d,%d,%ld,%f,%f\n", rank, com_size, size, sum, endTime - startTime);
}
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