This applies smpi_shared_malloc automatically to all allocations above a certain size.
Test based on the sample-shared one: malloc >= 8bytes is indeed shared, without using macros.
todo: calloc and co.
#ifndef SAMPI_H_
#define SAMPI_H_
+#define SAMPI_OVERRIDEN_MALLOC
#include <stdlib.h>
#include <smpi/smpi.h>
type _XBT_CONCAT(AP, name) args;
#ifndef HAVE_SMPI
+#undef malloc
+#undef free
// Internally disable these overrides (HAVE_SMPI is only defined when building the library)
#define malloc(nbytes) _sampi_malloc(nbytes)
#define calloc(n_elm, elm_size) _sampi_calloc((n_elm), (elm_size))
SMPI_SAMPLE_LOOP(loop_init, (loop_end), (loop_iter), 1, (iters), (thres))
#define SMPI_SAMPLE_DELAY(duration) for(smpi_execute(duration); 0; )
#define SMPI_SAMPLE_FLOPS(flops) for(smpi_execute_flops(flops); 0; )
-
XBT_PUBLIC void* smpi_shared_malloc(size_t size, const char* file, int line);
#define SMPI_SHARED_MALLOC(size) smpi_shared_malloc((size), __FILE__, __LINE__)
XBT_PUBLIC void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int nb_shared_blocks);
#define SMPI_PARTIAL_SHARED_MALLOC(size, shared_block_offsets, nb_shared_blocks) \
smpi_shared_malloc_partial((size), (shared_block_offsets), (nb_shared_blocks))
-XBT_PUBLIC void smpi_shared_free(void* data);
+
#define SMPI_SHARED_FREE(data) smpi_shared_free(data)
XBT_PUBLIC int smpi_shared_known_call(const char* func, const char* input);
#define getopt(x, y, z) smpi_getopt((x), (y), (z))
#define getopt_long(x, y, z, a, b) smpi_getopt_long((x), (y), (z), (a), (b))
#define getopt_long_only(x, y, z, a, b) smpi_getopt_long_only((x), (y), (z), (a), (b))
-
+#ifndef SAMPI_OVERRIDEN_MALLOC
+#define malloc(x) smpi_shared_malloc_intercept(x, __FILE__, __LINE__)
+#define free(x) smpi_shared_free(x)
+#endif
#endif
int* opt_index);
int smpi_getopt(int argc, char* const* argv, const char* options);
+void* smpi_shared_malloc_intercept(size_t size, const char* file, int line);
+void smpi_shared_free(void* data);
#ifdef __cplusplus
} // extern "C"
#endif
simgrid::config::declare_flag<double>("smpi/shared-malloc-blocksize",
"Size of the bogus file which will be created for global shared allocations",
1UL << 20);
+ simgrid::config::declare_flag<double>("smpi/auto-shared-malloc-thresh",
+ "Threshold size for the automatic sharing of memory",
+ 0);
+
simgrid::config::declare_flag<std::string>("smpi/shared-malloc-hugepage",
"Path to a mounted hugetlbfs, to use huge pages with shared malloc.", "");
return mem;
}
+
+void *smpi_shared_malloc_intercept(size_t size, const char *file, int line) {
+ if( simgrid::config::get_value<double>("smpi/auto-shared-malloc-thresh") == 0 || size < simgrid::config::get_value<double>("smpi/auto-shared-malloc-thresh"))
+ return ::operator new(size);
+ else
+ return smpi_shared_malloc(size, file, line);
+}
+
void *smpi_shared_malloc(size_t size, const char *file, int line) {
if (size > 0 && smpi_cfg_shared_malloc == SharedMallocType::LOCAL) {
return smpi_shared_malloc_local(size, file, line);
snprintf(loc, PTR_STRLEN, "%p", ptr);
auto meta = allocs_metadata.find(ptr);
if (meta == allocs_metadata.end()) {
- XBT_WARN("Cannot free: %p was not shared-allocated by SMPI - maybe its size was 0?", ptr);
+ if (simgrid::config::get_value<double>("smpi/auto_shared_malloc_thresh") > 0)//this free belongs to a malloc under the threshold.
+ ::operator delete(ptr);
+ else
+ XBT_WARN("Cannot free: %p was not shared-allocated by SMPI - maybe its size was 0?", ptr);
return;
}
shared_data_t* data = &meta->second.data->second;
endforeach()
if(NOT WIN32)
- foreach(x macro-shared macro-partial-shared macro-partial-shared-communication )
+ foreach(x macro-shared auto-shared macro-partial-shared macro-partial-shared-communication )
add_executable (${x} EXCLUDE_FROM_ALL ${x}/${x}.c)
target_link_libraries(${x} simgrid)
set_target_properties(${x} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
foreach(x coll-allgather coll-allgatherv coll-allreduce coll-alltoall coll-alltoallv coll-barrier coll-bcast
coll-gather coll-reduce coll-reduce-scatter coll-scatter macro-sample pt2pt-dsend pt2pt-pingpong
type-hvector type-indexed type-struct type-vector bug-17132 gh-139 timers privatization
- macro-shared macro-partial-shared macro-partial-shared-communication
+ macro-shared auto-shared macro-partial-shared macro-partial-shared-communication
io-simple io-simple-at io-all io-all-at io-shared io-ordered)
set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.c)
if(enable_smpi)
if(NOT WIN32)
ADD_TESH_FACTORIES(tesh-smpi-macro-shared "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/macro-shared --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/macro-shared macro-shared.tesh)
+ ADD_TESH_FACTORIES(tesh-smpi-auto-shared "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/auto-shared --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/auto-shared auto-shared.tesh)
ADD_TESH_FACTORIES(tesh-smpi-macro-partial-shared "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/macro-partial-shared --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/macro-partial-shared macro-partial-shared.tesh)
ADD_TESH_FACTORIES(tesh-smpi-macro-partial-shared-communication "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/macro-partial-shared-communication --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/macro-partial-shared-communication macro-partial-shared-communication.tesh)
endif()
--- /dev/null
+/* Copyright (c) 2009-2019. The SimGrid Team.
+ * All rights reserved. */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+/* This example should be instructive to learn about SMPI_SHARED_CALL */
+
+#include <stdio.h>
+#include <mpi.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+static void* hash(const char *str, uint64_t* ans)
+{
+ const char *tohash = str;
+ *ans=5381;
+ printf("hashing !\n");
+ int c = *tohash;
+ while (c != 0) {
+ *ans = ((*ans << 5) + *ans) + c; /* hash * 33 + c */
+ tohash++;
+ c = *tohash;
+ }
+ return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+ MPI_Init(&argc, &argv);
+ int rank;
+ int size;
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
+ //Let's Allocate a memory buffer
+ uint64_t* buf = malloc(sizeof(uint64_t));
+ //one writes data in it
+ if(rank==0){
+ *buf=size;
+ }
+
+ MPI_Barrier(MPI_COMM_WORLD);
+ //everyone reads from it.
+ printf("[%d] The value in the shared buffer is: %" PRIu64"\n", rank, *buf);
+
+ MPI_Barrier(MPI_COMM_WORLD);
+ //Try SMPI_SHARED_CALL function, which should call hash only once and for all.
+ static const char str[] = "onceandforall";
+ if(rank==size-1){
+ SMPI_SHARED_CALL(hash,str,str,buf);
+ }
+
+ MPI_Barrier(MPI_COMM_WORLD);
+
+ printf("[%d] After change, the value in the shared buffer is: %" PRIu64"\n", rank, *buf);
+
+ free(buf);
+
+ MPI_Finalize();
+ return 0;
+}
--- /dev/null
+p Test compute
+! output sort
+! timeout 5
+$ ${bindir:=.}/../../../smpi_script/bin/smpirun -hostfile ../hostfile -platform ../../../examples/platforms/small_platform.xml -np 4 ${bindir:=.}/auto-shared --log=smpi_kernel.thres:warning --log=xbt_cfg.thres:warning --cfg=smpi/auto-shared-malloc-thresh:8
+> [0] After change, the value in the shared buffer is: 16053117601147974045
+> [0] The value in the shared buffer is: 4
+> [1] After change, the value in the shared buffer is: 16053117601147974045
+> [1] The value in the shared buffer is: 4
+> [2] After change, the value in the shared buffer is: 16053117601147974045
+> [2] The value in the shared buffer is: 4
+> [3] After change, the value in the shared buffer is: 16053117601147974045
+> [3] The value in the shared buffer is: 4
+> hashing !
+
+! output sort
+! timeout 5
+$ ${bindir:=.}/../../../smpi_script/bin/smpirun -hostfile ../hostfile -platform ../../../examples/platforms/small_platform.xml -np 4 ${bindir:=.}/auto-shared --log=smpi_kernel.thres:warning --log=xbt_cfg.thres:warning --cfg=smpi/shared-malloc:local --cfg=smpi/auto-shared-malloc-thresh:8
+> [0] After change, the value in the shared buffer is: 16053117601147974045
+> [0] The value in the shared buffer is: 4
+> [1] After change, the value in the shared buffer is: 16053117601147974045
+> [1] The value in the shared buffer is: 4
+> [2] After change, the value in the shared buffer is: 16053117601147974045
+> [2] The value in the shared buffer is: 4
+> [3] After change, the value in the shared buffer is: 16053117601147974045
+> [3] The value in the shared buffer is: 4
+> hashing !
+
+