Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add/intel mpi example #20

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions example/hello-world-intel-mpi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Hello World Intel MPI

This example shows requesting using the intel MPI snippet, which means adding a custom setup and run
command to your code. An hpc-* flavored family is required (which is the default).

```bash
GOOGLE_PROJECT=myproject
snakemake --jobs 1 --executor googlebatch --googlebatch-region us-central1 --googlebatch-project ${GOOGLE_PROJECT} --default-storage-provider s3 --default-storage-prefix s3://my-snakemake-testing --googlebatch-snippets intel-mpi
```

57 changes: 57 additions & 0 deletions example/hello-world-intel-mpi/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# https://github.com/snakemake/snakemake/blob/main/tests/test_slurm_mpi/Snakefile
# Note that in reality, the mpi, account, and partition resources should be specified
# via --default-resources, in order to keep such infrastructure specific details out of the
# workflow definition.


localrules:
all,
clean,
copy,

rule all:
input:
"pi.calc",

rule clean:
shell:
"rm -f pi.calc"

rule copy:
input:
local("pi_MPI.c"),
output:
"pi_MPI.c",
log:
"logs/copy.log",
resources:
mem_mb=0,
shell:
"cp {input} {output} &> {log}"

rule compile:
input:
"pi_MPI.c",
output:
"pi_MPI",
log:
"logs/compile.log",
resources:
mem_mb=0,
shell:
"mpicc -o {output} {input} &> {log}"

rule calc_pi:
input:
"pi_MPI",
output:
"pi.calc",
log:
"logs/calc_pi.log",
resources:
mem_mb=0,
tasks=1,
mpi="mpiexec",
shell:
"chmod +x {input};"
"{resources.mpi} -hostfile $BATCH_HOSTS_FILE -n {resources.tasks} {input} 10 > {output};"
66 changes: 66 additions & 0 deletions example/hello-world-intel-mpi/pi_MPI.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <math.h>
#include "stdlib.h"
#include "mpi.h"


double f(double x){
return ( 4.0/(1.0 + x*x) );
}


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

int myrank, size;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

long long int i, n;
double PI25DT = 3.141592653589793238462643;
double mypi, pi, h, mysum, x;



/* Argument handling from command line */
if( 2 == argc ){
n = atoll(argv[1]);
}else{
if( 0 == myrank ){
n = 10000000;
printf("Too many or no argument given; using n = %d instead.\n", n);
}
}


MPI_Bcast(&n, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);

h = 1.0/( (double)n );
mysum = 0.0;

for(i = myrank+1 ; i <= n ; i += size){
x = h*((double)i - 0.5);
mysum = mysum + f(x);
}

mypi = h*mysum;

MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

/* Output result if myrank==0 */
if( 0 == myrank ){
char* filename = argv[1];
FILE *fp;
char buf[0x100];
snprintf(buf, sizeof(buf), "%s", filename);
fp = fopen(buf, "w+");
fprintf(fp, "\nUsing %d processes and the value n = %d.\n",size,n);
fprintf(fp, "Calculated pi: %.16f, with error %.16f\n\n", pi, fabs(pi - PI25DT));
fclose(fp);
}

MPI_Finalize();
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ source /opt/intel/mpi/latest/env/vars.sh
if [ $BATCH_TASK_INDEX = 0 ]; then
ls
which mpirun
echo "{% if resources.mpi %}{{ resources.mpi }}{% else %}mpiexec{% endif %} -hostfile $BATCH_HOSTS_FILE -n {{ settings.tasks }} -ppn {{ settings.tasks_per_node }} {{ command }}"
{% if resources.mpi %}{{ resources.mpi }}{% else %}mpiexec{% endif %} -hostfile $BATCH_HOSTS_FILE -n {{ settings.tasks }} -ppn {{ settings.tasks_per_node }} {{ command }}
echo "{{ command }}"
{{ command }}
fi
Loading