Skip to content

Commit

Permalink
add ability to output to file
Browse files Browse the repository at this point in the history
  • Loading branch information
plavin committed Apr 22, 2024
1 parent a94bc86 commit c8153e1
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/sst/elements/ariel/tests/testMPI/reduce.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mpi.h>
#include <omp.h>
#include <chrono>
#include <iostream>
#include "arielapi.h"

#define DEBUG 0
#define TIMING 0

int main(int argc, char* argv[]) {

Expand All @@ -24,7 +26,6 @@ int main(int argc, char* argv[]) {
exit(1);
}


int rank = 0;
int nranks = 0;

Expand All @@ -46,37 +47,58 @@ int main(int argc, char* argv[]) {

len = len / nranks;

FILE *output = stdout;

if (argc > 2) {
int len = strlen(argv[2]) + 12;// Space for underscore, plus up to a 10 digit integer, plus the null character
char *outfile = (char*)malloc(len);
if (!outfile) {
printf("Error allocating space for filename\n");
}
snprintf(outfile, len, "%s_%d", argv[2], rank);

output = fopen(outfile, "w");
if (!output) {
printf("Unable to open %s\n", outfile);
exit(1);
}
}


int nthreads = omp_get_max_threads();

#if DEBUG
printf("Running on %d ranks, %d threads per rank\n", nranks, nthreads);
#endif


// Initialize
int *vec = (int*) malloc(sizeof(int) * len);
for (int i = 0; i < len; i++) {
vec[i] = rank*len + i;
}

ariel_enable();

#if TIMING
auto begin = std::chrono::high_resolution_clock::now();
#endif

long int sum = 0;
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < len; i++) {
sum += vec[i];
}

#if TIMING
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count();
if (rank == 0) {
std::cout << nranks << " " << nthreads << " " << duration/1000000 << "\n";
}
ariel_disable();

#if DEBUG
printf("Rank %d: sum is %ld\n", rank, sum);
#endif

ariel_disable();

long int tot = 0;
MPI_Allreduce(
&sum,
Expand All @@ -86,11 +108,7 @@ int main(int argc, char* argv[]) {
MPI_SUM,
MPI_COMM_WORLD);


#if DEBUG
printf("Rank %d: tot is %ld\n", rank, tot);
#endif

fprintf(output, "Rank %d partial sum is %ld, total sum is %d\n", rank, sum, tot);

MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
Expand Down

0 comments on commit c8153e1

Please sign in to comment.