Skip to content

Commit

Permalink
fix mpi
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Applencourt committed Jun 17, 2024
1 parent 59caff0 commit 5c48364
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 11 deletions.
55 changes: 46 additions & 9 deletions mpi/gen_mpi.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
require_relative 'mpi_model'

puts <<~EOF
#include <stdint.h>
#include <mpi.h>
#include "mpi_tracepoints.h"
EOF

def common_block(c, provider)
params = c.parameters.collect(&:name)
tp_params = c.parameters.collect do |p|
Expand All @@ -31,10 +25,10 @@ def common_block(c, provider)
if c.has_return_type?
puts <<EOF
#{c.type} _retval;
_retval = P#{c.name}(#{params.join(', ')});
_retval = #{MPI_POINTER_NAMES[c]}(#{params.join(', ')});
EOF
else
puts " P#{c.name}(#{params.join(', ')});"
puts " #{MPI_POINTER_NAMES[c]}(#{params.join(', ')});"
end
c.tracepoint_parameters.each do |p|
puts p.init if p.after?
Expand All @@ -50,14 +44,57 @@ def normal_wrapper(c, provider)
puts <<~EOF
#{c.decl} {
EOF
puts " _init_tracer();" if c.init?

common_block(c, provider)
puts <<~EOF
}
EOF
end

def define_and_find_mpi_symbols()

$mpi_commands.each { |c|
puts <<EOF
#define #{MPI_POINTER_NAMES[c]} #{c.pointer_name}
#{c.decl_pointer(c.pointer_type_name)};
static #{c.pointer_type_name} #{MPI_POINTER_NAMES[c]} = (void *) 0x0;
EOF
}

puts <<EOF
static void find_mpi_symbols(void * handle, int verbose) {
EOF
$mpi_commands.each { |c|
puts <<EOF
#{MPI_POINTER_NAMES[c]} = (#{c.pointer_type_name})(intptr_t)dlsym(handle, "#{c.name}");
if (!#{MPI_POINTER_NAMES[c]} && verbose)
fprintf(stderr, "THAPI: Missing symbol #{c.name}!\\n");
EOF
}

puts <<EOF
}
EOF
end

puts <<~EOF
#include <stdint.h>
#include <mpi.h>
#include "mpi_tracepoints.h"
#include <dlfcn.h>
#include <pthread.h>
EOF

define_and_find_mpi_symbols

puts File::read(File.join(SRC_DIR,"tracer_mpi_helpers.include.c"))

$mpi_commands.each do |c|
next if c.name.start_with?("PMPI")
normal_wrapper(c, :lttng_ust_mpi)
end
4 changes: 2 additions & 2 deletions mpi/mpi_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

mpi_funcs_e = $mpi_api["functions"]

INIT_FUNCTIONS=/None/
INIT_FUNCTIONS=/MPI_Init|MPI_Init_thread/

$mpi_meta_parameters = YAML::load_file(File.join(SRC_DIR, "mpi_meta_parameters.yaml"))
$mpi_meta_parameters.fetch("meta_parameters",[]).each { |func, list|
Expand All @@ -39,7 +39,7 @@
}

def upper_snake_case(str)
str.gsub(/([A-Z][A-Z0-9]*)/, '_\1').upcase
str.gsub(/([a-z][a-z0-9]*)/, '_\1').upcase
end

MPI_POINTER_NAMES = $mpi_commands.collect { |c|
Expand Down
48 changes: 48 additions & 0 deletions mpi/tracer_mpi_helpers.include.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
static pthread_once_t _init = PTHREAD_ONCE_INIT;
static __thread volatile int in_init = 0;
static volatile unsigned int _initialized = 0;

static void _load_tracer(void) {
char *s = NULL;
void *handle = NULL;
int verbose = 0;

s = getenv("LTTNG_UST_MPI_LIBMPI_LOADER");
if (s)
handle = dlopen(s, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
else
handle = dlopen("libmpi.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
if (handle) {
void* ptr = dlsym(handle, "MPI_Init");
if (ptr == (void*)&MPI_Init) { //opening oneself
dlclose(handle);
handle = NULL;
}
}

if( !handle ) {
fprintf(stderr, "THAPI: Failure: could not load MPI library!\n");
exit(1);
}

s = getenv("LTTNG_UST_MPI_VERBOSE");
if (s)
verbose = 1;

find_mpi_symbols(handle, verbose);
}

static inline void _init_tracer(void) {
if( __builtin_expect (_initialized, 1) )
return;
/* Avoid reentrancy */
if (!in_init) {
in_init=1;
__sync_synchronize();
pthread_once(&_init, _load_tracer);
__sync_synchronize();
in_init=0;
}
_initialized = 1;
}

0 comments on commit 5c48364

Please sign in to comment.