Skip to content

Commit 673ad34

Browse files
authored
feat: add/intel mpi example (#20)
This adds the intel mpi example. The example functionally works, but the actual command does not write the output to file (it is empty). I pinged @johanneskoester about this and likely it needs to be debugged (the workflow is also in core snakemake) but technically speaking the google batch portion of this is working as expected so I think we can PR here. My credits for this project are expiring early January (and I haven't used a ton) so we need to keep moving. --------- Signed-off-by: vsoch <vsoch@users.noreply.github.com> Co-authored-by: vsoch <vsoch@users.noreply.github.com>
1 parent 043701d commit 673ad34

4 files changed

Lines changed: 135 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Hello World Intel MPI
2+
3+
This example shows requesting using the intel MPI snippet, which means adding a custom setup and run
4+
command to your code. An hpc-* flavored family is required (which is the default).
5+
6+
```bash
7+
GOOGLE_PROJECT=myproject
8+
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
9+
```
10+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# https://github.com/snakemake/snakemake/blob/main/tests/test_slurm_mpi/Snakefile
2+
# Note that in reality, the mpi, account, and partition resources should be specified
3+
# via --default-resources, in order to keep such infrastructure specific details out of the
4+
# workflow definition.
5+
6+
7+
localrules:
8+
all,
9+
clean,
10+
copy,
11+
12+
rule all:
13+
input:
14+
"pi.calc",
15+
16+
rule clean:
17+
shell:
18+
"rm -f pi.calc"
19+
20+
rule copy:
21+
input:
22+
local("pi_MPI.c"),
23+
output:
24+
"pi_MPI.c",
25+
log:
26+
"logs/copy.log",
27+
resources:
28+
mem_mb=0,
29+
shell:
30+
"cp {input} {output} &> {log}"
31+
32+
rule compile:
33+
input:
34+
"pi_MPI.c",
35+
output:
36+
"pi_MPI",
37+
log:
38+
"logs/compile.log",
39+
resources:
40+
mem_mb=0,
41+
shell:
42+
"mpicc -o {output} {input} &> {log}"
43+
44+
rule calc_pi:
45+
input:
46+
"pi_MPI",
47+
output:
48+
"pi.calc",
49+
log:
50+
"logs/calc_pi.log",
51+
resources:
52+
mem_mb=0,
53+
tasks=1,
54+
mpi="mpiexec",
55+
shell:
56+
"chmod +x {input};"
57+
"{resources.mpi} -hostfile $BATCH_HOSTS_FILE -n {resources.tasks} {input} 10 > {output};"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
#include "stdlib.h"
4+
#include "mpi.h"
5+
6+
7+
double f(double x){
8+
return ( 4.0/(1.0 + x*x) );
9+
}
10+
11+
12+
int main(int argc,char *argv[])
13+
{
14+
15+
int myrank, size;
16+
17+
MPI_Init(&argc, &argv);
18+
MPI_Comm_size(MPI_COMM_WORLD, &size);
19+
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
20+
21+
long long int i, n;
22+
double PI25DT = 3.141592653589793238462643;
23+
double mypi, pi, h, mysum, x;
24+
25+
26+
27+
/* Argument handling from command line */
28+
if( 2 == argc ){
29+
n = atoll(argv[1]);
30+
}else{
31+
if( 0 == myrank ){
32+
n = 10000000;
33+
printf("Too many or no argument given; using n = %d instead.\n", n);
34+
}
35+
}
36+
37+
38+
MPI_Bcast(&n, 1, MPI_LONG_LONG_INT, 0, MPI_COMM_WORLD);
39+
40+
h = 1.0/( (double)n );
41+
mysum = 0.0;
42+
43+
for(i = myrank+1 ; i <= n ; i += size){
44+
x = h*((double)i - 0.5);
45+
mysum = mysum + f(x);
46+
}
47+
48+
mypi = h*mysum;
49+
50+
MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
51+
52+
/* Output result if myrank==0 */
53+
if( 0 == myrank ){
54+
char* filename = argv[1];
55+
FILE *fp;
56+
char buf[0x100];
57+
snprintf(buf, sizeof(buf), "%s", filename);
58+
fp = fopen(buf, "w+");
59+
fprintf(fp, "\nUsing %d processes and the value n = %d.\n",size,n);
60+
fprintf(fp, "Calculated pi: %.16f, with error %.16f\n\n", pi, fabs(pi - PI25DT));
61+
fclose(fp);
62+
}
63+
64+
MPI_Finalize();
65+
return 0;
66+
}

snakemake_executor_plugin_googlebatch/snippets/intel-mpi/run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ source /opt/intel/mpi/latest/env/vars.sh
88
if [ $BATCH_TASK_INDEX = 0 ]; then
99
ls
1010
which mpirun
11-
echo "{% if resources.mpi %}{{ resources.mpi }}{% else %}mpiexec{% endif %} -hostfile $BATCH_HOSTS_FILE -n {{ settings.tasks }} -ppn {{ settings.tasks_per_node }} {{ command }}"
12-
{% if resources.mpi %}{{ resources.mpi }}{% else %}mpiexec{% endif %} -hostfile $BATCH_HOSTS_FILE -n {{ settings.tasks }} -ppn {{ settings.tasks_per_node }} {{ command }}
11+
echo "{{ command }}"
12+
{{ command }}
1313
fi

0 commit comments

Comments
 (0)