From: Arnaud Giersch Date: Wed, 11 Oct 2017 09:08:39 +0000 (+0200) Subject: Replace some malloc/free by C++ new/delete. X-Git-Tag: v3.18~494 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/9de2143df74699fb6e21a9eb8f37e38db0c00357 Replace some malloc/free by C++ new/delete. --- diff --git a/include/xbt/string.hpp b/include/xbt/string.hpp index 912fe8dd6a..14c9696c5c 100644 --- a/include/xbt/string.hpp +++ b/include/xbt/string.hpp @@ -14,10 +14,11 @@ #if SIMGRID_HAVE_MC -#include +#include #include #include #include +#include #include @@ -71,7 +72,7 @@ public: ~string() { if (string_data::data != &NUL) - std::free(string_data::data); + delete[] string_data::data; } // Ctors @@ -82,8 +83,8 @@ public: string_data::data = const_cast(&NUL); } else { string_data::len = size; - string_data::data = static_cast(std::malloc(string_data::len + 1)); - memcpy(string_data::data, s, string_data::len); + string_data::data = new char[string_data::len + 1]; + std::copy_n(s, string_data::len, string_data::data); string_data::data[string_data::len] = '\0'; } } @@ -103,14 +104,14 @@ public: void assign(const char* s, size_t size) { if (string_data::data != &NUL) { - std::free(string_data::data); + delete[] string_data::data; string_data::data = nullptr; string_data::len = 0; } if (size != 0) { string_data::len = size; - string_data::data = (char*) std::malloc(string_data::len + 1); - std::memcpy(string_data::data, s, string_data::len); + string_data::data = new char[string_data::len + 1]; + std::copy_n(s, string_data::len, string_data::data); string_data::data[string_data::len] = '\0'; } } diff --git a/src/mc/mc_smx.cpp b/src/mc/mc_smx.cpp index 4711074164..72b087c652 100644 --- a/src/mc/mc_smx.cpp +++ b/src/mc/mc_smx.cpp @@ -66,7 +66,7 @@ static void MC_process_refresh_simix_actor_dynar(simgrid::mc::RemoteClient* proc s_xbt_dynar_t dynar; process->read_bytes(&dynar, sizeof(dynar), remote_dynar); - smx_actor_t* data = (smx_actor_t*)malloc(dynar.elmsize * dynar.used); + smx_actor_t* data = static_cast(::operator new(dynar.elmsize * dynar.used)); process->read_bytes(data, dynar.elmsize * dynar.used, dynar.data); // Load each element of the vector from the MCed process: @@ -78,7 +78,7 @@ static void MC_process_refresh_simix_actor_dynar(simgrid::mc::RemoteClient* proc process->read_bytes(&info.copy, sizeof(info.copy), remote(data[i])); target.push_back(std::move(info)); } - free(data); + ::operator delete(data); } namespace simgrid { namespace mc { diff --git a/src/mc/mc_snapshot.cpp b/src/mc/mc_snapshot.cpp index ce1c2a8263..13586a6b52 100644 --- a/src/mc/mc_snapshot.cpp +++ b/src/mc/mc_snapshot.cpp @@ -123,8 +123,8 @@ int MC_snapshot_region_memcmp( bool stack_alloc = size < 64; const bool region1_need_buffer = region1==nullptr || region1->storage_type()==simgrid::mc::StorageType::Flat; const bool region2_need_buffer = region2==nullptr || region2->storage_type()==simgrid::mc::StorageType::Flat; - void* buffer1a = region1_need_buffer ? nullptr : stack_alloc ? alloca(size) : malloc(size); - void* buffer2a = region2_need_buffer ? nullptr : stack_alloc ? alloca(size) : malloc(size); + void* buffer1a = region1_need_buffer ? nullptr : stack_alloc ? alloca(size) : ::operator new(size); + void* buffer2a = region2_need_buffer ? nullptr : stack_alloc ? alloca(size) : ::operator new(size); const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size); const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size); int res; @@ -133,8 +133,8 @@ int MC_snapshot_region_memcmp( else res = memcmp(buffer1, buffer2, size); if (not stack_alloc) { - free(buffer1a); - free(buffer2a); + ::operator delete(buffer1a); + ::operator delete(buffer2a); } return res; } diff --git a/src/msg/msg_io.cpp b/src/msg/msg_io.cpp index 592312a0e1..fd4ad9a032 100644 --- a/src/msg/msg_io.cpp +++ b/src/msg/msg_io.cpp @@ -478,10 +478,11 @@ void *MSG_storage_get_data(msg_storage_t storage) xbt_dict_t MSG_storage_get_content(msg_storage_t storage) { std::map* content = storage->getContent(); - xbt_dict_t content_as_dict = xbt_dict_new_homogeneous(xbt_free_f); + // Note: ::operator delete is ok here (no destructor called) since the dict elements are of POD type sg_size_t. + xbt_dict_t content_as_dict = xbt_dict_new_homogeneous(::operator delete); for (auto const& entry : *content) { - sg_size_t* psize = static_cast(malloc(sizeof(sg_size_t))); + sg_size_t* psize = new sg_size_t; *psize = entry.second; xbt_dict_set(content_as_dict, entry.first.c_str(), psize, nullptr); } diff --git a/src/msg/msg_private.hpp b/src/msg/msg_private.hpp index b30226e7ff..c1affe16fe 100644 --- a/src/msg/msg_private.hpp +++ b/src/msg/msg_private.hpp @@ -28,11 +28,7 @@ public: /********************************* Task **************************************/ struct s_simdata_task_t { - ~s_simdata_task_t() - { - /* parallel tasks only */ - xbt_free(this->host_list); - } + ~s_simdata_task_t() { delete[] this->host_list; /* parallel tasks only */ } void setUsed(); void setNotUsed() { this->isused = false; } diff --git a/src/msg/msg_task.cpp b/src/msg/msg_task.cpp index c9b9fda0f0..f07beb0c02 100644 --- a/src/msg/msg_task.cpp +++ b/src/msg/msg_task.cpp @@ -5,6 +5,7 @@ #include "msg_private.hpp" #include "src/simix/smx_private.hpp" +#include extern "C" { @@ -90,12 +91,11 @@ msg_task_t MSG_parallel_task_create(const char *name, int host_nb, const msg_hos /* Simulator Data specific to parallel tasks */ simdata->host_nb = host_nb; - simdata->host_list = xbt_new0(sg_host_t, host_nb); + simdata->host_list = new sg_host_t[host_nb]; simdata->flops_parallel_amount = flops_amount; simdata->bytes_parallel_amount = bytes_amount; - for (int i = 0; i < host_nb; i++) - simdata->host_list[i] = host_list[i]; + std::copy_n(host_list, host_nb, simdata->host_list); return task; } diff --git a/src/smpi/colls/allgatherv/allgatherv-ompi-bruck.cpp b/src/smpi/colls/allgatherv/allgatherv-ompi-bruck.cpp index 2c170ed603..d57fd3f633 100644 --- a/src/smpi/colls/allgatherv/allgatherv-ompi-bruck.cpp +++ b/src/smpi/colls/allgatherv/allgatherv-ompi-bruck.cpp @@ -89,8 +89,6 @@ int Coll_allgatherv_ompi_bruck::allgatherv(void *sbuf, int scount, { int sendto, recvfrom, blockcount, i; unsigned int distance; - int *new_rcounts = NULL, *new_rdispls = NULL; - int *new_scounts = NULL, *new_sdispls = NULL; ptrdiff_t slb, rlb, sext, rext; char *tmpsend = NULL, *tmprecv = NULL; MPI_Datatype new_rdtype = MPI_DATATYPE_NULL, new_sdtype = MPI_DATATYPE_NULL; @@ -126,10 +124,10 @@ int Coll_allgatherv_ompi_bruck::allgatherv(void *sbuf, int scount, - blockcount doubles until the last step when only the remaining data is exchanged. */ - new_rcounts = (int*) calloc(4*size, sizeof(int)); - new_rdispls = new_rcounts + size; - new_scounts = new_rdispls + size; - new_sdispls = new_scounts + size; + int* new_rcounts = new int[4 * size]; + int* new_rdispls = new_rcounts + size; + int* new_scounts = new_rdispls + size; + int* new_sdispls = new_scounts + size; for (distance = 1; distance < size; distance<<=1) { @@ -170,7 +168,7 @@ int Coll_allgatherv_ompi_bruck::allgatherv(void *sbuf, int scount, } - free(new_rcounts); + delete[] new_rcounts; return MPI_SUCCESS; diff --git a/src/smpi/colls/barrier/barrier-ompi.cpp b/src/smpi/colls/barrier/barrier-ompi.cpp index 727febc25d..0f4eec52f9 100644 --- a/src/smpi/colls/barrier/barrier-ompi.cpp +++ b/src/smpi/colls/barrier/barrier-ompi.cpp @@ -265,7 +265,7 @@ int Coll_barrier_ompi_basic_linear::barrier(MPI_Comm comm) else { MPI_Request* requests; - requests = (MPI_Request*)malloc( size * sizeof(MPI_Request) ); + requests = new MPI_Request[size]; for (i = 1; i < size; ++i) { requests[i] = Request::irecv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE, COLL_TAG_BARRIER, comm @@ -280,7 +280,7 @@ int Coll_barrier_ompi_basic_linear::barrier(MPI_Comm comm) ); } Request::waitall( size-1, requests+1, MPI_STATUSES_IGNORE ); - free( requests ); + delete[] requests; } /* All done */ diff --git a/src/smpi/colls/bcast/bcast-ompi-pipeline.cpp b/src/smpi/colls/bcast/bcast-ompi-pipeline.cpp index d7e48fd710..0de5fffa22 100644 --- a/src/smpi/colls/bcast/bcast-ompi-pipeline.cpp +++ b/src/smpi/colls/bcast/bcast-ompi-pipeline.cpp @@ -85,7 +85,7 @@ int Coll_bcast_ompi_pipeline::bcast( void* buffer, tmpbuf = (char *) buffer; if( tree->tree_nextsize != 0 ) { - send_reqs = xbt_new(MPI_Request, tree->tree_nextsize ); + send_reqs = new MPI_Request[tree->tree_nextsize]; } /* Root code */ @@ -205,8 +205,8 @@ int Coll_bcast_ompi_pipeline::bcast( void* buffer, Request::wait( &recv_reqs[req_index], MPI_STATUS_IGNORE ); } - if( NULL != send_reqs ) free(send_reqs); - xbt_free(tree); + delete[] send_reqs; + ompi_coll_tuned_topo_destroy_tree(&tree); return (MPI_SUCCESS); } diff --git a/src/smpi/colls/bcast/bcast-ompi-split-bintree.cpp b/src/smpi/colls/bcast/bcast-ompi-split-bintree.cpp index a2eb2dc1ce..2ab6c7d5b0 100644 --- a/src/smpi/colls/bcast/bcast-ompi-split-bintree.cpp +++ b/src/smpi/colls/bcast/bcast-ompi-split-bintree.cpp @@ -296,7 +296,7 @@ Coll_bcast_ompi_split_bintree::bcast ( void* buffer, comm, MPI_STATUS_IGNORE); } } - xbt_free(tree); + ompi_coll_tuned_topo_destroy_tree(&tree); return (MPI_SUCCESS); diff --git a/src/smpi/colls/coll_tuned_topo.cpp b/src/smpi/colls/coll_tuned_topo.cpp index 06a23b4019..7138c7ed90 100644 --- a/src/smpi/colls/coll_tuned_topo.cpp +++ b/src/smpi/colls/coll_tuned_topo.cpp @@ -101,7 +101,7 @@ ompi_coll_tuned_topo_build_tree( int fanout, size = comm->size(); rank = comm->rank(); - tree = (ompi_coll_tree_t*)malloc(sizeof(ompi_coll_tree_t)); + tree = new ompi_coll_tree_t; if (not tree) { XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory"); return NULL; @@ -201,7 +201,7 @@ ompi_coll_tuned_topo_build_in_order_bintree( MPI_Comm comm ) size = comm->size(); rank = comm->rank(); - tree = (ompi_coll_tree_t*)malloc(sizeof(ompi_coll_tree_t)); + tree = new ompi_coll_tree_t; if (not tree) { XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory"); return NULL; @@ -304,7 +304,7 @@ int ompi_coll_tuned_topo_destroy_tree( ompi_coll_tree_t** tree ) ptr = *tree; - free (ptr); + delete ptr; *tree = NULL; /* mark tree as gone */ return MPI_SUCCESS; @@ -345,7 +345,7 @@ ompi_coll_tuned_topo_build_bmtree( MPI_Comm comm, index = rank -root; - bmtree = (ompi_coll_tree_t*)malloc(sizeof(ompi_coll_tree_t)); + bmtree = new ompi_coll_tree_t; if (not bmtree) { XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory"); return NULL; @@ -424,7 +424,7 @@ ompi_coll_tree_t* ompi_coll_tuned_topo_build_in_order_bmtree(MPI_Comm comm, int vrank = (rank - root + size) % size; - bmtree = (ompi_coll_tree_t*)xbt_malloc(sizeof(ompi_coll_tree_t)); + bmtree = new ompi_coll_tree_t; if (not bmtree) { XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory"); return NULL; @@ -494,7 +494,7 @@ ompi_coll_tuned_topo_build_chain( int fanout, /* * Allocate space for topology arrays if needed */ - chain = (ompi_coll_tree_t*)malloc( sizeof(ompi_coll_tree_t) ); + chain = new ompi_coll_tree_t; if (not chain) { XBT_DEBUG("coll:tuned:topo:build_chain PANIC out of memory"); fflush(stdout); diff --git a/src/smpi/colls/gather/gather-ompi.cpp b/src/smpi/colls/gather/gather-ompi.cpp index 4510e088cb..3e4e65c83e 100644 --- a/src/smpi/colls/gather/gather-ompi.cpp +++ b/src/smpi/colls/gather/gather-ompi.cpp @@ -181,7 +181,7 @@ int Coll_gather_ompi_binomial::gather(void* sbuf, int scount, MPI_Datatype sdtyp /* other non-leaf nodes */ smpi_free_tmp_buffer(tempbuf); } - xbt_free(bmtree); + ompi_coll_tuned_topo_destroy_tree(&bmtree); return MPI_SUCCESS; err_hndl: @@ -266,8 +266,8 @@ int Coll_gather_ompi_linear_sync::gather(void *sbuf, int scount, - Waitall for all the second segments to complete. */ char* ptmp; - MPI_Request *reqs = NULL, first_segment_req; - reqs = (MPI_Request*)calloc(size, sizeof(MPI_Request)); + MPI_Request first_segment_req; + MPI_Request* reqs = new (std::nothrow) MPI_Request[size]; if (NULL == reqs) { ret = -1; line = __LINE__; @@ -319,7 +319,7 @@ int Coll_gather_ompi_linear_sync::gather(void *sbuf, int scount, ret = Request::waitall(size, reqs, MPI_STATUSES_IGNORE); if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl; } - free(reqs); + delete[] reqs; } /* All done */ diff --git a/src/smpi/colls/reduce/reduce-ompi.cpp b/src/smpi/colls/reduce/reduce-ompi.cpp index 25e62ec0c5..a4b540e809 100644 --- a/src/smpi/colls/reduce/reduce-ompi.cpp +++ b/src/smpi/colls/reduce/reduce-ompi.cpp @@ -261,10 +261,7 @@ int smpi_coll_tuned_ompi_reduce_generic( void* sendbuf, void* recvbuf, int origi else { int creq = 0; - MPI_Request* sreq = NULL; - - sreq = (MPI_Request*) calloc( max_outstanding_reqs, - sizeof(MPI_Request ) ); + MPI_Request* sreq = new (std::nothrow) MPI_Request[max_outstanding_reqs]; if (NULL == sreq) { line = __LINE__; ret = -1; goto error_hndl; } /* post first group of requests */ @@ -303,10 +300,10 @@ int smpi_coll_tuned_ompi_reduce_generic( void* sendbuf, void* recvbuf, int origi MPI_STATUSES_IGNORE ); /* free requests */ - free(sreq); + delete[] sreq; } } - free(tree); + ompi_coll_tuned_topo_destroy_tree(&tree); return MPI_SUCCESS; error_hndl: /* error handler */ diff --git a/src/smpi/colls/reduce/reduce-rab.cpp b/src/smpi/colls/reduce/reduce-rab.cpp index b399292eeb..5b2f4b20fb 100644 --- a/src/smpi/colls/reduce/reduce-rab.cpp +++ b/src/smpi/colls/reduce/reduce-rab.cpp @@ -613,14 +613,14 @@ static int MPI_I_anyReduce(void* Sendbuf, void* Recvbuf, int count, MPI_Datatype MPI_Type_extent(mpi_datatype, &typelng); scrlng = typelng * count; #ifdef NO_CACHE_OPTIMIZATION - scr1buf = static_cast(xbt_malloc(scrlng)); - scr2buf = static_cast(xbt_malloc(scrlng)); - scr3buf = static_cast(xbt_malloc(scrlng)); + scr1buf = new char[scrlng]; + scr2buf = new char[scrlng]; + scr3buf = new char[scrlng]; #else # ifdef SCR_LNG_OPTIM scrlng = SCR_LNG_OPTIM(scrlng); # endif - scr2buf = static_cast(xbt_malloc(3*scrlng)); /* To test cache problems. */ + scr2buf = new char[3 * scrlng]; /* To test cache problems. */ scr1buf = scr2buf + 1*scrlng; /* scr1buf and scr3buf must not*/ scr3buf = scr2buf + 2*scrlng; /* be used for malloc because */ /* they are interchanged below.*/ @@ -932,9 +932,11 @@ static int MPI_I_anyReduce(void* Sendbuf, void* Recvbuf, int count, MPI_Datatype } # ifdef NO_CACHE_TESTING - xbt_free(scr1buf); xbt_free(scr2buf); xbt_free(scr3buf); + delete[] scr1buf; + delete[] scr2buf; + delete[] scr3buf; # else - xbt_free(scr2buf); /* scr1buf and scr3buf are part of scr2buf */ + delete[] scr2buf; /* scr1buf and scr3buf are part of scr2buf */ # endif return(MPI_SUCCESS); } /* new_prot */ diff --git a/src/smpi/colls/scatter/scatter-ompi.cpp b/src/smpi/colls/scatter/scatter-ompi.cpp index 89737b61c5..6c14004a71 100644 --- a/src/smpi/colls/scatter/scatter-ompi.cpp +++ b/src/smpi/colls/scatter/scatter-ompi.cpp @@ -159,7 +159,7 @@ int Coll_scatter_ompi_binomial::scatter(void* sbuf, int scount, MPI_Datatype sdt if (NULL != tempbuf) smpi_free_tmp_buffer(tempbuf); // not FIXME : store the tree, as done in ompi, instead of calculating it each time ? - xbt_free(bmtree); + ompi_coll_tuned_topo_destroy_tree(&bmtree); return MPI_SUCCESS; diff --git a/src/smpi/colls/smpi_mvapich2_selector.cpp b/src/smpi/colls/smpi_mvapich2_selector.cpp index 01fb26857e..2d60cf160f 100644 --- a/src/smpi/colls/smpi_mvapich2_selector.cpp +++ b/src/smpi/colls/smpi_mvapich2_selector.cpp @@ -795,7 +795,7 @@ int Coll_reduce_scatter_mvapich2::reduce_scatter(void *sendbuf, void *recvbuf, i int range = 0; int range_threshold = 0; int is_commutative = 0; - int *disps = static_cast(xbt_malloc(comm_size * sizeof (int))); + int* disps = new int[comm_size]; if(mv2_red_scat_thresholds_table==NULL) init_mv2_reduce_scatter_tables_stampede(); @@ -853,7 +853,7 @@ int Coll_reduce_scatter_mvapich2::reduce_scatter(void *sendbuf, void *recvbuf, i recvcnts, datatype, op, comm); } - xbt_free(disps); + delete[] disps; return mpi_errno; } @@ -998,28 +998,27 @@ int Coll_scatter_mvapich2::scatter(void *sendbuf, void smpi_coll_cleanup_mvapich2() { - int i = 0; if (mv2_alltoall_thresholds_table) - xbt_free(mv2_alltoall_thresholds_table[i]); - xbt_free(mv2_alltoall_thresholds_table); - xbt_free(mv2_size_alltoall_tuning_table); - xbt_free(mv2_alltoall_table_ppn_conf); + delete[] mv2_alltoall_thresholds_table[0]; + delete[] mv2_alltoall_thresholds_table; + delete[] mv2_size_alltoall_tuning_table; + delete[] mv2_alltoall_table_ppn_conf; - xbt_free(mv2_gather_thresholds_table); + delete[] mv2_gather_thresholds_table; if (mv2_allgather_thresholds_table) - xbt_free(mv2_allgather_thresholds_table[0]); - xbt_free(mv2_size_allgather_tuning_table); - xbt_free(mv2_allgather_table_ppn_conf); - xbt_free(mv2_allgather_thresholds_table); - - xbt_free(mv2_allgatherv_thresholds_table); - xbt_free(mv2_reduce_thresholds_table); - xbt_free(mv2_red_scat_thresholds_table); - xbt_free(mv2_allreduce_thresholds_table); - xbt_free(mv2_bcast_thresholds_table); + delete[] mv2_allgather_thresholds_table[0]; + delete[] mv2_size_allgather_tuning_table; + delete[] mv2_allgather_table_ppn_conf; + delete[] mv2_allgather_thresholds_table; + + delete[] mv2_allgatherv_thresholds_table; + delete[] mv2_reduce_thresholds_table; + delete[] mv2_red_scat_thresholds_table; + delete[] mv2_allreduce_thresholds_table; + delete[] mv2_bcast_thresholds_table; if (mv2_scatter_thresholds_table) - xbt_free(mv2_scatter_thresholds_table[0]); - xbt_free(mv2_scatter_thresholds_table); - xbt_free(mv2_size_scatter_tuning_table); - xbt_free(mv2_scatter_table_ppn_conf); + delete[] mv2_scatter_thresholds_table[0]; + delete[] mv2_scatter_thresholds_table; + delete[] mv2_size_scatter_tuning_table; + delete[] mv2_scatter_table_ppn_conf; } diff --git a/src/smpi/colls/smpi_mvapich2_selector_stampede.hpp b/src/smpi/colls/smpi_mvapich2_selector_stampede.hpp index a922738a50..24cdf97b2a 100644 --- a/src/smpi/colls/smpi_mvapich2_selector_stampede.hpp +++ b/src/smpi/colls/smpi_mvapich2_selector_stampede.hpp @@ -9,6 +9,11 @@ /************ Alltoall variables and initializers */ +#ifndef SMPI_MVAPICH2_SELECTOR_STAMPEDE_HPP +#define SMPI_MVAPICH2_SELECTOR_STAMPEDE_HPP + +#include + #define MV2_MAX_NB_THRESHOLDS 32 XBT_PUBLIC(void) smpi_coll_cleanup_mvapich2(void); @@ -45,18 +50,15 @@ mv2_alltoall_tuning_table** mv2_alltoall_thresholds_table = NULL; static void init_mv2_alltoall_tables_stampede() { - int i; int agg_table_sum = 0; mv2_alltoall_tuning_table** table_ptrs = NULL; mv2_alltoall_num_ppn_conf = 3; if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; - mv2_alltoall_thresholds_table = static_cast( - xbt_malloc(sizeof(mv2_alltoall_tuning_table*) * mv2_alltoall_num_ppn_conf)); - table_ptrs = static_cast( - xbt_malloc(sizeof(mv2_alltoall_tuning_table*) * mv2_alltoall_num_ppn_conf)); - mv2_size_alltoall_tuning_table = static_cast(xbt_malloc(sizeof(int) * mv2_alltoall_num_ppn_conf)); - mv2_alltoall_table_ppn_conf = static_cast(xbt_malloc(mv2_alltoall_num_ppn_conf * sizeof(int))); + mv2_alltoall_thresholds_table = new mv2_alltoall_tuning_table*[mv2_alltoall_num_ppn_conf]; + table_ptrs = new mv2_alltoall_tuning_table*[mv2_alltoall_num_ppn_conf]; + mv2_size_alltoall_tuning_table = new int[mv2_alltoall_num_ppn_conf]; + mv2_alltoall_table_ppn_conf = new int[mv2_alltoall_num_ppn_conf]; mv2_alltoall_table_ppn_conf[0] = 1; mv2_size_alltoall_tuning_table[0] = 6; mv2_alltoall_tuning_table mv2_tmp_alltoall_thresholds_table_1ppn[] = { @@ -314,19 +316,16 @@ static void init_mv2_alltoall_tables_stampede() }; table_ptrs[2] = mv2_tmp_alltoall_thresholds_table_16ppn; agg_table_sum = 0; - for (i = 0; i < mv2_alltoall_num_ppn_conf; i++) { + for (int i = 0; i < mv2_alltoall_num_ppn_conf; i++) { agg_table_sum += mv2_size_alltoall_tuning_table[i]; } - mv2_alltoall_thresholds_table[0] = - static_cast(xbt_malloc(agg_table_sum * sizeof(mv2_alltoall_tuning_table))); - memcpy(mv2_alltoall_thresholds_table[0], table_ptrs[0], - (sizeof(mv2_alltoall_tuning_table) * mv2_size_alltoall_tuning_table[0])); - for (i = 1; i < mv2_alltoall_num_ppn_conf; i++) { + mv2_alltoall_thresholds_table[0] = new mv2_alltoall_tuning_table[agg_table_sum]; + std::copy_n(table_ptrs[0], mv2_size_alltoall_tuning_table[0], mv2_alltoall_thresholds_table[0]); + for (int i = 1; i < mv2_alltoall_num_ppn_conf; i++) { mv2_alltoall_thresholds_table[i] = mv2_alltoall_thresholds_table[i - 1] + mv2_size_alltoall_tuning_table[i - 1]; - memcpy(mv2_alltoall_thresholds_table[i], table_ptrs[i], - (sizeof(mv2_alltoall_tuning_table) * mv2_size_alltoall_tuning_table[i])); + std::copy_n(table_ptrs[i], mv2_size_alltoall_tuning_table[i], mv2_alltoall_thresholds_table[i]); } - xbt_free(table_ptrs); + delete[] table_ptrs; } /************ Allgather variables and initializers */ @@ -366,19 +365,16 @@ static int MPIR_Allgather_RD_Allgather_Comm_MV2(void* sendbuf, int sendcount, MP static void init_mv2_allgather_tables_stampede() { - int i; int agg_table_sum = 0; if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; mv2_allgather_tuning_table** table_ptrs = NULL; mv2_allgather_num_ppn_conf = 3; - mv2_allgather_thresholds_table = static_cast( - xbt_malloc(sizeof(mv2_allgather_tuning_table*) * mv2_allgather_num_ppn_conf)); - table_ptrs = static_cast( - xbt_malloc(sizeof(mv2_allgather_tuning_table*) * mv2_allgather_num_ppn_conf)); - mv2_size_allgather_tuning_table = static_cast(xbt_malloc(sizeof(int) * mv2_allgather_num_ppn_conf)); - mv2_allgather_table_ppn_conf = static_cast(xbt_malloc(mv2_allgather_num_ppn_conf * sizeof(int))); + mv2_allgather_thresholds_table = new mv2_allgather_tuning_table*[mv2_allgather_num_ppn_conf]; + table_ptrs = new mv2_allgather_tuning_table*[mv2_allgather_num_ppn_conf]; + mv2_size_allgather_tuning_table = new int[mv2_allgather_num_ppn_conf]; + mv2_allgather_table_ppn_conf = new int[mv2_allgather_num_ppn_conf]; mv2_allgather_table_ppn_conf[0] = 1; mv2_size_allgather_tuning_table[0] = 6; mv2_allgather_tuning_table mv2_tmp_allgather_thresholds_table_1ppn[] = { @@ -550,19 +546,16 @@ static void init_mv2_allgather_tables_stampede() }; table_ptrs[2] = mv2_tmp_allgather_thresholds_table_16ppn; agg_table_sum = 0; - for (i = 0; i < mv2_allgather_num_ppn_conf; i++) { + for (int i = 0; i < mv2_allgather_num_ppn_conf; i++) { agg_table_sum += mv2_size_allgather_tuning_table[i]; } - mv2_allgather_thresholds_table[0] = - static_cast(xbt_malloc(agg_table_sum * sizeof(mv2_allgather_tuning_table))); - memcpy(mv2_allgather_thresholds_table[0], table_ptrs[0], - (sizeof(mv2_allgather_tuning_table) * mv2_size_allgather_tuning_table[0])); - for (i = 1; i < mv2_allgather_num_ppn_conf; i++) { + mv2_allgather_thresholds_table[0] = new mv2_allgather_tuning_table[agg_table_sum]; + std::copy_n(table_ptrs[0], mv2_size_allgather_tuning_table[0], mv2_allgather_thresholds_table[0]); + for (int i = 1; i < mv2_allgather_num_ppn_conf; i++) { mv2_allgather_thresholds_table[i] = mv2_allgather_thresholds_table[i - 1] + mv2_size_allgather_tuning_table[i - 1]; - memcpy(mv2_allgather_thresholds_table[i], table_ptrs[i], - (sizeof(mv2_allgather_tuning_table) * mv2_size_allgather_tuning_table[i])); + std::copy_n(table_ptrs[i], mv2_size_allgather_tuning_table[i], mv2_allgather_thresholds_table[i]); } - xbt_free(table_ptrs); + delete[] table_ptrs; } /************ Gather variables and initializers */ @@ -601,8 +594,7 @@ static void init_mv2_gather_tables_stampede() if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; mv2_size_gather_tuning_table = 7; - mv2_gather_thresholds_table = - static_cast(xbt_malloc(mv2_size_gather_tuning_table * sizeof(mv2_gather_tuning_table))); + mv2_gather_thresholds_table = new mv2_gather_tuning_table[mv2_size_gather_tuning_table]; mv2_gather_tuning_table mv2_tmp_gather_thresholds_table[] = { {16, 2, @@ -653,8 +645,7 @@ static void init_mv2_gather_tables_stampede() {{0, -1, &MPIR_Gather_intra}}}, }; - memcpy(mv2_gather_thresholds_table, mv2_tmp_gather_thresholds_table, - mv2_size_gather_tuning_table * sizeof(mv2_gather_tuning_table)); + std::copy_n(mv2_tmp_gather_thresholds_table, mv2_size_gather_tuning_table, mv2_gather_thresholds_table); } /************ Allgatherv variables and initializers */ @@ -687,8 +678,7 @@ static void init_mv2_allgatherv_tables_stampede() if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; mv2_size_allgatherv_tuning_table = 6; - mv2_allgatherv_thresholds_table = static_cast( - xbt_malloc(mv2_size_allgatherv_tuning_table * sizeof(mv2_allgatherv_tuning_table))); + mv2_allgatherv_thresholds_table = new mv2_allgatherv_tuning_table[mv2_size_allgatherv_tuning_table]; mv2_allgatherv_tuning_table mv2_tmp_allgatherv_thresholds_table[] = { { 16, @@ -734,8 +724,7 @@ static void init_mv2_allgatherv_tables_stampede() }, }; - memcpy(mv2_allgatherv_thresholds_table, mv2_tmp_allgatherv_thresholds_table, - mv2_size_allgatherv_tuning_table * sizeof(mv2_allgatherv_tuning_table)); + std::copy_n(mv2_tmp_allgatherv_thresholds_table, mv2_size_allgatherv_tuning_table, mv2_allgatherv_thresholds_table); } /************ Allreduce variables and initializers */ @@ -801,8 +790,7 @@ static void init_mv2_allreduce_tables_stampede() if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; mv2_size_allreduce_tuning_table = 8; - mv2_allreduce_thresholds_table = static_cast( - xbt_malloc(mv2_size_allreduce_tuning_table * sizeof(mv2_allreduce_tuning_table))); + mv2_allreduce_thresholds_table = new mv2_allreduce_tuning_table[mv2_size_allreduce_tuning_table]; mv2_allreduce_tuning_table mv2_tmp_allreduce_thresholds_table[] = { { 16, @@ -927,8 +915,7 @@ static void init_mv2_allreduce_tables_stampede() }, }; - memcpy(mv2_allreduce_thresholds_table, mv2_tmp_allreduce_thresholds_table, - mv2_size_allreduce_tuning_table * sizeof(mv2_allreduce_tuning_table)); + std::copy_n(mv2_tmp_allreduce_thresholds_table, mv2_size_allreduce_tuning_table, mv2_allreduce_thresholds_table); } struct mv2_bcast_tuning_element { @@ -987,8 +974,7 @@ static void init_mv2_bcast_tables_stampede() if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; mv2_size_bcast_tuning_table = 8; - mv2_bcast_thresholds_table = - static_cast(xbt_malloc(mv2_size_bcast_tuning_table * sizeof(mv2_bcast_tuning_table))); + mv2_bcast_thresholds_table = new mv2_bcast_tuning_table[mv2_size_bcast_tuning_table]; mv2_bcast_tuning_table mv2_tmp_bcast_thresholds_table[] = { {16, @@ -1156,8 +1142,7 @@ static void init_mv2_bcast_tables_stampede() {32768, 524288, &MPIR_Shmem_Bcast_MV2, -1}, {524288, -1, &MPIR_Shmem_Bcast_MV2, -1}}}}; - memcpy(mv2_bcast_thresholds_table, mv2_tmp_bcast_thresholds_table, - mv2_size_bcast_tuning_table * sizeof(mv2_bcast_tuning_table)); + std::copy_n(mv2_tmp_bcast_thresholds_table, mv2_size_bcast_tuning_table, mv2_bcast_thresholds_table); } /************ Reduce variables and initializers */ @@ -1205,8 +1190,7 @@ static void init_mv2_reduce_tables_stampede() simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; /*Stampede*/ mv2_size_reduce_tuning_table = 8; - mv2_reduce_thresholds_table = - static_cast(xbt_malloc(mv2_size_reduce_tuning_table * sizeof(mv2_reduce_tuning_table))); + mv2_reduce_thresholds_table = new mv2_reduce_tuning_table[mv2_size_reduce_tuning_table]; mv2_reduce_tuning_table mv2_tmp_reduce_thresholds_table[] = { { 16, @@ -1390,8 +1374,7 @@ static void init_mv2_reduce_tables_stampede() }, }; - memcpy(mv2_reduce_thresholds_table, mv2_tmp_reduce_thresholds_table, - mv2_size_reduce_tuning_table * sizeof(mv2_reduce_tuning_table)); + std::copy_n(mv2_tmp_reduce_thresholds_table, mv2_size_reduce_tuning_table, mv2_reduce_thresholds_table); } /************ Reduce scatter variables and initializers */ @@ -1431,8 +1414,7 @@ static void init_mv2_reduce_scatter_tables_stampede() if (simgrid::smpi::Colls::smpi_coll_cleanup_callback == NULL) simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; mv2_size_red_scat_tuning_table = 6; - mv2_red_scat_thresholds_table = static_cast( - xbt_malloc(mv2_size_red_scat_tuning_table * sizeof(mv2_red_scat_tuning_table))); + mv2_red_scat_thresholds_table = new mv2_red_scat_tuning_table[mv2_size_red_scat_tuning_table]; mv2_red_scat_tuning_table mv2_tmp_red_scat_thresholds_table[] = { { 16, @@ -1484,8 +1466,7 @@ static void init_mv2_reduce_scatter_tables_stampede() }, }; - memcpy(mv2_red_scat_thresholds_table, mv2_tmp_red_scat_thresholds_table, - mv2_size_red_scat_tuning_table * sizeof(mv2_red_scat_tuning_table)); + std::copy_n(mv2_tmp_red_scat_thresholds_table, mv2_size_red_scat_tuning_table, mv2_red_scat_thresholds_table); } /************ Scatter variables and initializers */ @@ -1535,15 +1516,12 @@ static void init_mv2_scatter_tables_stampede() simgrid::smpi::Colls::smpi_coll_cleanup_callback = &smpi_coll_cleanup_mvapich2; int agg_table_sum = 0; - int i; mv2_scatter_tuning_table** table_ptrs = NULL; mv2_scatter_num_ppn_conf = 3; - mv2_scatter_thresholds_table = - static_cast(xbt_malloc(sizeof(mv2_scatter_tuning_table*) * mv2_scatter_num_ppn_conf)); - table_ptrs = - static_cast(xbt_malloc(sizeof(mv2_scatter_tuning_table*) * mv2_scatter_num_ppn_conf)); - mv2_size_scatter_tuning_table = static_cast(xbt_malloc(sizeof(int) * mv2_scatter_num_ppn_conf)); - mv2_scatter_table_ppn_conf = static_cast(xbt_malloc(mv2_scatter_num_ppn_conf * sizeof(int))); + mv2_scatter_thresholds_table = new mv2_scatter_tuning_table*[mv2_scatter_num_ppn_conf]; + table_ptrs = new mv2_scatter_tuning_table*[mv2_scatter_num_ppn_conf]; + mv2_size_scatter_tuning_table = new int[mv2_scatter_num_ppn_conf]; + mv2_scatter_table_ppn_conf = new int[mv2_scatter_num_ppn_conf]; mv2_scatter_table_ppn_conf[0] = 1; mv2_size_scatter_tuning_table[0] = 6; mv2_scatter_tuning_table mv2_tmp_scatter_thresholds_table_1ppn[] = { @@ -1822,17 +1800,16 @@ static void init_mv2_scatter_tables_stampede() }; table_ptrs[2] = mv2_tmp_scatter_thresholds_table_16ppn; agg_table_sum = 0; - for (i = 0; i < mv2_scatter_num_ppn_conf; i++) { + for (int i = 0; i < mv2_scatter_num_ppn_conf; i++) { agg_table_sum += mv2_size_scatter_tuning_table[i]; } - mv2_scatter_thresholds_table[0] = - static_cast(xbt_malloc(agg_table_sum * sizeof(mv2_scatter_tuning_table))); - memcpy(mv2_scatter_thresholds_table[0], table_ptrs[0], - (sizeof(mv2_scatter_tuning_table) * mv2_size_scatter_tuning_table[0])); - for (i = 1; i < mv2_scatter_num_ppn_conf; i++) { + mv2_scatter_thresholds_table[0] = new mv2_scatter_tuning_table[agg_table_sum]; + std::copy_n(table_ptrs[0], mv2_size_scatter_tuning_table[0], mv2_scatter_thresholds_table[0]); + for (int i = 1; i < mv2_scatter_num_ppn_conf; i++) { mv2_scatter_thresholds_table[i] = mv2_scatter_thresholds_table[i - 1] + mv2_size_scatter_tuning_table[i - 1]; - memcpy(mv2_scatter_thresholds_table[i], table_ptrs[i], - (sizeof(mv2_scatter_tuning_table) * mv2_size_scatter_tuning_table[i])); + std::copy_n(table_ptrs[i], mv2_size_scatter_tuning_table[i], mv2_scatter_thresholds_table[i]); } - xbt_free(table_ptrs); + delete[] table_ptrs; } + +#endif diff --git a/src/smpi/include/smpi_group.hpp b/src/smpi/include/smpi_group.hpp index 73803641d1..4637dea46f 100644 --- a/src/smpi/include/smpi_group.hpp +++ b/src/smpi/include/smpi_group.hpp @@ -8,6 +8,7 @@ #define SMPI_GROUP_HPP_INCLUDED #include "smpi_f2c.hpp" +#include namespace simgrid{ namespace smpi{ diff --git a/src/smpi/include/smpi_keyvals.hpp b/src/smpi/include/smpi_keyvals.hpp index bd4dd80fdf..48f60ac4a4 100644 --- a/src/smpi/include/smpi_keyvals.hpp +++ b/src/smpi/include/smpi_keyvals.hpp @@ -7,7 +7,6 @@ #define SMPI_KEYVALS_HPP_INCLUDED #include "smpi/smpi.h" -#include "xbt/ex.hpp" #include @@ -54,7 +53,7 @@ class Keyval{ template int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){ - smpi_key_elem value = (smpi_key_elem)xbt_new0(s_smpi_key_elem_t, 1); + smpi_key_elem value = new s_smpi_key_elem_t; value->copy_fn=copy_fn; value->delete_fn=delete_fn; @@ -75,7 +74,7 @@ template int Keyval::keyval_free(int* keyval){ } if(elem->refcount==1){ T::keyvals_.erase(*keyval); - xbt_free(elem); + delete elem; }else{ elem->refcount--; } diff --git a/src/smpi/internals/smpi_memory.cpp b/src/smpi/internals/smpi_memory.cpp index 7811d9570a..7ac4abd024 100644 --- a/src/smpi/internals/smpi_memory.cpp +++ b/src/smpi/internals/smpi_memory.cpp @@ -146,8 +146,7 @@ void smpi_initialize_global_memory_segments() return; } - smpi_privatization_regions = static_cast( - xbt_malloc(smpi_process_count() * sizeof(s_smpi_privatization_region_t))); + smpi_privatization_regions = new s_smpi_privatization_region_t[smpi_process_count()]; for (int i=0; i< smpi_process_count(); i++){ // create SIMIX_process_count() mappings of this size with the same data inside @@ -215,7 +214,7 @@ void smpi_destroy_global_memory_segments(){ XBT_WARN("Unmapping of fd %d failed: %s", smpi_privatization_regions[i].file_descriptor, strerror(errno)); close(smpi_privatization_regions[i].file_descriptor); } - xbt_free(smpi_privatization_regions); + delete[] smpi_privatization_regions; #endif } diff --git a/src/smpi/internals/smpi_shared.cpp b/src/smpi/internals/smpi_shared.cpp index 5d41bd600f..9f30550067 100644 --- a/src/smpi/internals/smpi_shared.cpp +++ b/src/smpi/internals/smpi_shared.cpp @@ -67,6 +67,7 @@ namespace{ class smpi_source_location : public std::string { public: + smpi_source_location() = default; smpi_source_location(const char* filename, int line) : std::string(std::string(filename) + ":" + std::to_string(line)) { } @@ -238,16 +239,15 @@ void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int xbt_free(huge_page_filename); } if(smpi_shared_malloc_bogusfile == -1) { - char *name = xbt_strdup("/tmp/simgrid-shmalloc-XXXXXX"); + char name[] = "/tmp/simgrid-shmalloc-XXXXXX"; smpi_shared_malloc_bogusfile = mkstemp(name); XBT_DEBUG("bogusfile : %s\n", name); unlink(name); - xbt_free(name); - char* dumb = (char*)calloc(1, smpi_shared_malloc_blocksize); + char* dumb = new char[smpi_shared_malloc_blocksize](); // zero initialized ssize_t err = write(smpi_shared_malloc_bogusfile, dumb, smpi_shared_malloc_blocksize); if(err<0) xbt_die("Could not write bogus file for shared malloc"); - xbt_free(dumb); + delete[] dumb; } int mmap_base_flag = MAP_FIXED | MAP_SHARED | MAP_POPULATE; @@ -313,7 +313,7 @@ void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int shared_metadata_t newmeta; //register metadata for memcpy avoidance - shared_data_key_type* data = (shared_data_key_type*)xbt_malloc(sizeof(shared_data_key_type)); + shared_data_key_type* data = new shared_data_key_type; data->second.fd = -1; data->second.count = 1; newmeta.size = size; @@ -346,8 +346,8 @@ void *smpi_shared_malloc(size_t size, const char *file, int line) { size_t shared_block_offsets[2] = {0, size}; return smpi_shared_malloc_partial(size, shared_block_offsets, nb_shared_blocks); } - XBT_DEBUG("Classic malloc %zu", size); - return xbt_malloc(size); + XBT_DEBUG("Classic allocation of %zu bytes", size); + return ::operator new(size); } int smpi_is_shared(void* ptr, std::vector> &private_blocks, size_t *offset){ @@ -441,13 +441,13 @@ void smpi_shared_free(void *ptr) if (meta != allocs_metadata.end()){ meta->second.data->second.count--; if(meta->second.data->second.count==0) - xbt_free(meta->second.data); + delete meta->second.data; } munmap(ptr, meta->second.size); } else { - XBT_DEBUG("Classic free of %p", ptr); - xbt_free(ptr); + XBT_DEBUG("Classic deallocation of %p", ptr); + ::operator delete(ptr); } } #endif diff --git a/src/smpi/mpi/smpi_datatype_derived.cpp b/src/smpi/mpi/smpi_datatype_derived.cpp index ba77e82eef..3be87a2df3 100644 --- a/src/smpi/mpi/smpi_datatype_derived.cpp +++ b/src/smpi/mpi/smpi_datatype_derived.cpp @@ -6,6 +6,7 @@ #include "smpi_datatype_derived.hpp" #include "smpi_op.hpp" +#include XBT_LOG_EXTERNAL_CATEGORY(smpi_datatype); diff --git a/src/smpi/mpi/smpi_group.cpp b/src/smpi/mpi/smpi_group.cpp index a3b3446dc4..d621068a1e 100644 --- a/src/smpi/mpi/smpi_group.cpp +++ b/src/smpi/mpi/smpi_group.cpp @@ -3,8 +3,9 @@ /* 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. */ -#include "smpi_comm.hpp" #include "smpi_group.hpp" +#include "smpi_comm.hpp" +#include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)"); @@ -224,7 +225,7 @@ int Group::excl(int n, int *ranks, MPI_Group * newgroup){ int oldsize = size_; int newsize = oldsize - n; *newgroup = new Group(newsize); - int* to_exclude=xbt_new0(int, size_); + int* to_exclude = new int[size_]; for (int i = 0; i < oldsize; i++) to_exclude[i]=0; for (int i = 0; i < n; i++) @@ -237,7 +238,7 @@ int Group::excl(int n, int *ranks, MPI_Group * newgroup){ j++; } } - xbt_free(to_exclude); + delete[] to_exclude; return MPI_SUCCESS; }