8 #ifndef VCLUSTER_UNIT_TEST_UTIL_HPP_ 
    9 #define VCLUSTER_UNIT_TEST_UTIL_HPP_ 
   11 #include "Point_test.hpp" 
   12 #include "VCluster_base.hpp" 
   13 #include "Vector/vector_test_util.hpp" 
   15 #define RECEIVE_UNKNOWN 1 
   16 #define RECEIVE_SIZE_UNKNOWN 2 
   21 #define N_LOOP 67108864 
   22 #define BUFF_STEP 524288 
   26 size_t global_step = 0;
 
   35 int mod(
int x, 
int m) {
 
   43 void * msg_alloc(
size_t msg_i ,
size_t total_msg, 
size_t total_p, 
size_t i,
size_t ri, 
void * ptr)
 
   51     if (create_vcluster().getProcessingUnits() <= 8)
 
   52     {
if (totp_check) BOOST_REQUIRE_EQUAL(total_p,create_vcluster().getProcessingUnits()-1);}
 
   54     {
if (totp_check) BOOST_REQUIRE_EQUAL(total_p,(
size_t)8);}
 
   56     BOOST_REQUIRE_EQUAL(msg_i, global_step);
 
   63     v->get(i).resize(msg_i);
 
   66     return &(v->get(i).get(0));
 
   76 void * msg_alloc2(
size_t msg_i ,
size_t total_msg, 
size_t total_p, 
size_t i, 
size_t ri, 
void * ptr)
 
   81     prc_recv.resize(total_p);
 
   83     BOOST_REQUIRE_EQUAL(msg_i, global_step);
 
   86     v->get(
id-1).resize(msg_i);
 
   87     prc_recv.get(
id-1) = i;
 
   88     return &(v->get(
id-1).get(0));
 
   91 void * msg_alloc3(
size_t msg_i ,
size_t total_msg, 
size_t total_p, 
size_t i, 
size_t ri, 
void * ptr)
 
   99     BOOST_REQUIRE_EQUAL(msg_i, global_step);
 
  101     v->last().resize(msg_i);
 
  103     return &(v->last().get(0));
 
  106 template<
unsigned int ip, 
typename T> 
void commFunc(
Vcluster & vcl,
openfpm::vector< size_t > & prc, 
openfpm::vector< T > & data, 
void * (* msg_alloc)(
size_t,
size_t,
size_t,
size_t,
size_t,
void *), 
void * ptr_arg)
 
  111 template<
unsigned int ip, 
typename T> 
void commFunc_null_odd(
Vcluster & vcl,
openfpm::vector< size_t > & prc, 
openfpm::vector< T > & data, 
void * (* msg_alloc)(
size_t,
size_t,
size_t,
size_t,
size_t,
void *), 
void * ptr_arg)
 
  122 template <
unsigned int ip> std::string method()
 
  124     return std::string(
"NBX");
 
  127 template<
unsigned int ip> 
void test_no_send_some_peer()
 
  139     long int ps = n_proc / (8 + 1);
 
  152         for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  159                 std::ostringstream msg;
 
  161                 std::string str(msg.str());
 
  162                 message.last().resize(j);
 
  163                 memset(message.last().getPointer(),0,j);
 
  164                 std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
 
  169     recv_message.resize(n_proc);
 
  176     commFunc_null_odd<ip>(vcl,prc,message,msg_alloc,&recv_message);
 
  181     double clk_max = clk;
 
  183     size_t size_send_recv = 2 * j * (prc.size());
 
  184     vcl.sum(size_send_recv);
 
  188     if (vcl.getProcessUnitID() == 0)
 
  189         std::cout << 
"(Long pattern: " << method<ip>() << 
")Buffer size: " << j << 
"    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << 
" MB/s  " << 
"    Bandwidth (Total): " << size_send_recv / clk / 1e6 << 
" MB/s    Clock: " << clk << 
"   Clock MAX: " << clk_max <<
"\n";
 
  193     for (
long int i = 0 ; i < 8  && i < (
long int)n_proc ; i++)
 
  195         long int p_id = (- (i+1) * ps + (
long int)vcl.getProcessUnitID());
 
  199             p_id = p_id % n_proc;
 
  201         if (p_id != (
long int)vcl.getProcessUnitID())
 
  207             std::ostringstream msg;
 
  208             msg << 
"Hello from " << p_id << 
" to " << vcl.getProcessUnitID();
 
  209             std::string str(msg.str());
 
  210             BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),
true);
 
  214             BOOST_REQUIRE_EQUAL((
size_t)0,recv_message.get(p_id).size());
 
  219 template<
unsigned int ip> 
void test_known()
 
  230     for (
size_t s = 0 ; s < N_TRY ; s++)
 
  232         for (
size_t j = 32 ; j < N_LOOP ; j*=2)
 
  239             recv_message.reserve(n_proc);
 
  246             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  253                     std::ostringstream msg;
 
  255                     std::string str(msg.str());
 
  256                     message.last().resize(j);
 
  257                     memset(message.last().getPointer(),0,j);
 
  258                     std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
 
  262             recv_message.resize(n_proc);
 
  264             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  270                     p_id = p_id % n_proc;
 
  275                     recv_message.get(p_id).resize(j);
 
  291             double clk_max = clk;
 
  293             size_t size_send_recv = 2 * j * (prc.
size());
 
  294             vcl.
sum(size_send_recv);
 
  299                 std::cout << 
"(Short pattern: " << method<ip>() << 
")Buffer size: " << j << 
"    Bandwidth (Average): " << size_send_recv / vcl.
getProcessingUnits() / clk / 1e6 << 
" MB/s  " << 
"    Bandwidth (Total): " << size_send_recv / clk / 1e6 << 
" MB/s    Clock: " << clk << 
"   Clock MAX: " << clk_max <<
"\n";
 
  303             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  309                     p_id = p_id % n_proc;
 
  313                     std::ostringstream msg;
 
  315                     std::string str(msg.str());
 
  316                     BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),
true);
 
  320                     BOOST_REQUIRE_EQUAL((
size_t)0,recv_message.get(p_id).size());
 
  327 template<
unsigned int ip> 
void test(
unsigned int opt)
 
  338     for (
size_t s = 0 ; s < N_TRY ; s++)
 
  340         for (
size_t j = 32 ; j < N_LOOP ; j*=2)
 
  359             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  369                     std::ostringstream msg;
 
  371                     std::string str(msg.str());
 
  372                     message.last().resize(j);
 
  373                     memset(message.last().getPointer(),0,j);
 
  374                     std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
 
  379             recv_message.resize(n_proc);
 
  382             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  388                     p_id = p_id % n_proc;
 
  391                     recv_message.get(p_id).resize(j);
 
  394             if (opt == RECEIVE_UNKNOWN)
 
  400             else if (opt == RECEIVE_SIZE_UNKNOWN)
 
  407                 sz_send.resize(prc.
size());
 
  408                 ptr.resize(prc.
size());
 
  410                 for (
size_t i = 0 ; i < prc.
size() ; i++)
 
  412                     sz_send.get(i) = message.get(i).
size();
 
  413                     ptr.get(i) = &message.get(i).get(0);
 
  419                 for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  425                     {p_id = p_id % n_proc;}
 
  437                                                 &ptr.get(0),prc_recv.
size(),&prc_recv.get(0),msg_alloc,&recv_message);
 
  450             double clk_max = clk;
 
  452             size_t size_send_recv = 2 * j * (prc.
size());
 
  453             vcl.
sum(size_send_recv);
 
  458                 std::cout << 
"(Short pattern: " << method<ip>() << 
")Buffer size: " << j << 
"    Bandwidth (Average): " << size_send_recv / vcl.
getProcessingUnits() / clk / 1e6 << 
" MB/s  " << 
"    Bandwidth (Total): " << size_send_recv / clk / 1e6 << 
" MB/s    Clock: " << clk << 
"   Clock MAX: " << clk_max <<
"\n";
 
  462             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  468                     p_id = p_id % n_proc;
 
  472                     std::ostringstream msg;
 
  474                     std::string str(msg.str());
 
  475                     BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),
true);
 
  479                     BOOST_REQUIRE_EQUAL((
size_t)0,recv_message.get(p_id).size());
 
  484         if (opt == RECEIVE_SIZE_UNKNOWN)
 
  487         std::srand(create_vcluster().getProcessUnitID());
 
  488         std::default_random_engine eg;
 
  489         std::uniform_int_distribution<int> d(0,n_proc/8);
 
  493         for (
size_t j = 32 ; j < N_LOOP && n_proc < 16 ; j*=2)
 
  506             for (
size_t i = 0 ; i < n_proc ; i++)
 
  514                     message.last().fill(0);
 
  515                     std::ostringstream msg;
 
  517                     std::string str(msg.str());
 
  518                     message.last().resize(str.size());
 
  519                     std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
 
  520                     message.last().resize(j);
 
  533             commFunc<ip>(vcl,prc,message,msg_alloc3,&recv_message);
 
  538             double clk_max = clk;
 
  540             size_t size_send_recv = (prc.size() + recv_message.
size()) * j;
 
  541             vcl.sum(size_send_recv);
 
  545             clk /= vcl.getProcessingUnits();
 
  547             if (vcl.getProcessUnitID() == 0)
 
  548                 std::cout << 
"(Random Pattern: " << method<ip>() << 
") Buffer size: " << j << 
"    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << 
" MB/s  " << 
"    Bandwidth (Total): " << size_send_recv / clk / 1e6 <<  
" MB/s    Clock: " << clk << 
"   Clock MAX: " << clk_max << 
"\n";
 
  553             for (
size_t i = 0 ; i < recv_message.
size() ; i++)
 
  555                 std::ostringstream msg;
 
  556                 msg << 
"Hello from " << prc_recv.get(i) << 
" to " << vcl.getProcessUnitID();
 
  557                 std::string str(msg.str());
 
  558                 BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(i).get(0))),
true);
 
  567             for (
size_t i = 0 ; i < prc_recv.
size() ; i++)
 
  569                 prc.add(prc_recv.get(i));
 
  571                 std::ostringstream msg;
 
  572                 msg << 
"Hey from " << vcl.getProcessUnitID() << 
" to " << prc_recv.get(i);
 
  573                 std::string str(msg.str());
 
  574                 message.last().resize(str.size());
 
  575                 std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
 
  576                 message.last().resize(j);
 
  581             recv_message.clear();
 
  583             commFunc<ip>(vcl,prc,message,msg_alloc3,&recv_message);
 
  587             BOOST_REQUIRE_EQUAL(o_send.
size(),prc_recv.
size());
 
  589             for (
size_t i = 0 ; i < o_send.
size() ; i++)
 
  592                 for ( ; j < prc_recv.
size() ; j++)
 
  594                     if (o_send.get(i) == prc_recv.get(j))
 
  598                         std::ostringstream msg;
 
  599                         msg << 
"Hey from " << prc_recv.get(i) << 
" to " << vcl.getProcessUnitID();
 
  600                         std::string str(msg.str());
 
  601                         BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(i).get(0))),
true);
 
  606                 BOOST_REQUIRE_EQUAL(j != prc_recv.
size(),
true);
 
  612         for (
size_t j = 32 ; j < N_LOOP ; j*=2)
 
  616             long int ps = n_proc / (8 + 1);
 
  625             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  627                 size_t p_id = ((i+1) * ps + vcl.getProcessUnitID()) % n_proc;
 
  628                 if (p_id != vcl.getProcessUnitID())
 
  632                     std::ostringstream msg;
 
  633                     msg << 
"Hello from " << vcl.getProcessUnitID() << 
" to " << p_id;
 
  634                     std::string str(msg.str());
 
  635                     message.last().resize(j);
 
  636                     memset(message.last().getPointer(),0,j);
 
  637                     std::copy(str.c_str(),&(str.c_str())[msg.str().size()],&(message.last().get(0)));
 
  641             recv_message.resize(n_proc);
 
  643             for (
size_t i = 0 ; i < 8  && i < n_proc ; i++)
 
  645                 long int p_id = (- (i+1) * ps + (
long int)vcl.getProcessUnitID());
 
  649                     p_id = p_id % n_proc;
 
  651                 if (p_id != (
long int)vcl.getProcessUnitID())
 
  652                     recv_message.get(p_id).resize(j);
 
  660             commFunc<ip>(vcl,prc,message,msg_alloc,&recv_message);
 
  665             double clk_max = clk;
 
  667             size_t size_send_recv = 2 * j * (prc.size());
 
  668             vcl.sum(size_send_recv);
 
  672             if (vcl.getProcessUnitID() == 0)
 
  673                 std::cout << 
"(Long pattern: " << method<ip>() << 
")Buffer size: " << j << 
"    Bandwidth (Average): " << size_send_recv / vcl.getProcessingUnits() / clk / 1e6 << 
" MB/s  " << 
"    Bandwidth (Total): " << size_send_recv / clk / 1e6 << 
" MB/s    Clock: " << clk << 
"   Clock MAX: " << clk_max <<
"\n";
 
  677             for (
long int i = 0 ; i < 8  && i < (
long int)n_proc ; i++)
 
  679                 long int p_id = (- (i+1) * ps + (
long int)vcl.getProcessUnitID());
 
  683                     p_id = p_id % n_proc;
 
  685                 if (p_id != (
long int)vcl.getProcessUnitID())
 
  687                     std::ostringstream msg;
 
  688                     msg << 
"Hello from " << p_id << 
" to " << vcl.getProcessUnitID();
 
  689                     std::string str(msg.str());
 
  690                     BOOST_REQUIRE_EQUAL(std::equal(str.c_str(),str.c_str() + str.size() ,&(recv_message.get(p_id).get(0))),
true);
 
  694                     BOOST_REQUIRE_EQUAL((
size_t)0,recv_message.get(p_id).
size());
 
  707 void test_send_recv_complex(
const size_t n, 
Vcluster & vcl)
 
  717     for (
size_t i = 0 ; i < 8 ; i++)
 
  724     for (
size_t i = 0 ; i < 8 ; i++)
 
  726         pt_buf.get(i).resize(n);
 
  735     for (
size_t i = 0 ; i < 8 ; i++)
 
  737         for (
size_t j = 0 ; j < n ; j++)
 
  743             BOOST_REQUIRE_EQUAL(pt.template get<p::x>(),p_recv);
 
  744             BOOST_REQUIRE_EQUAL(pt.template get<p::y>(),p_recv);
 
  745             BOOST_REQUIRE_EQUAL(pt.template get<p::z>(),p_recv);
 
  746             BOOST_REQUIRE_EQUAL(pt.template get<p::s>(),p_recv);
 
  747             BOOST_REQUIRE_EQUAL(pt.template get<p::v>()[0],p_recv);
 
  748             BOOST_REQUIRE_EQUAL(pt.template get<p::v>()[1],p_recv);
 
  749             BOOST_REQUIRE_EQUAL(pt.template get<p::v>()[2],p_recv);
 
  750             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[0][0],p_recv);
 
  751             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[0][1],p_recv);
 
  752             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[0][2],p_recv);
 
  753             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[1][0],p_recv);
 
  754             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[1][1],p_recv);
 
  755             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[1][2],p_recv);
 
  756             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[2][0],p_recv);
 
  757             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[2][1],p_recv);
 
  758             BOOST_REQUIRE_EQUAL(pt.template get<p::t>()[2][2],p_recv);
 
  770 template<
typename T> 
void test_send_recv_primitives(
size_t n, 
Vcluster & vcl)
 
  778     for (
size_t i = 0 ; i < 8 ; i++)
 
  785     for (
size_t i = 0 ; i < 8 ; i++)
 
  787         pt_buf.get(i).resize(n);
 
  796     for (
size_t i = 0 ; i < 8 ; i++)
 
  798         for (
size_t j = 0 ; j < n ; j++)
 
  800             T pt = pt_buf.get(i).get(j);
 
  804             BOOST_REQUIRE_EQUAL(pt,p_recv);
 
  814     for (
size_t i = 0 ; i < 8 ; i++)
 
  821     for (
size_t i = 0 ; i < 8 ; i++)
 
  823         pt_buf.get(i).resize(n);
 
  832     for (
size_t i = 0 ; i < 8 ; i++)
 
  834         for (
size_t j = 0 ; j < n ; j++)
 
  836             T pt = pt_buf.get(i).get(j);
 
  840             BOOST_REQUIRE_EQUAL(pt,p_recv);
 
  847 template<
typename T>  
void test_single_all_gather_primitives(
Vcluster & vcl)
 
  858         BOOST_REQUIRE_EQUAL(i,(
size_t)clt.get(i));
 
  864 template<
typename T>  
void test_single_all_broadcast_primitives(
Vcluster & vcl)
 
  888     for (
size_t i = 0 ; i < bdata.
size() ; i++)
 
  889         BOOST_REQUIRE_EQUAL(i,(
size_t)bdata.get(i));
 
void sum(T &num)
Sum the numbers across all processors and get the result. 
 
size_t getProcessUnitID()
Get the process unit id. 
 
void execute()
Execute all the requests. 
 
bool Bcast(openfpm::vector< T, Mem, gr > &v, size_t root)
Broadcast the data to all processors. 
 
void max(T &num)
Get the maximum number across all processors (or reduction with infinity norm) 
 
void sendrecvMultipleMessagesNBX(openfpm::vector< size_t > &prc, openfpm::vector< T > &data, openfpm::vector< size_t > prc_recv, openfpm::vector< size_t > &recv_sz, void *(*msg_alloc)(size_t, size_t, size_t, size_t, size_t, void *), void *ptr_arg, long int opt=NONE)
Send and receive multiple messages. 
 
double getwct()
Return the elapsed real time. 
 
bool send(size_t proc, size_t tag, const void *mem, size_t sz)
Send data to a processor. 
 
Implementation of VCluster class. 
 
bool recv(size_t proc, size_t tag, void *v, size_t sz)
Recv data from a processor. 
 
void start()
Start the timer. 
 
Test structure used for several test. 
 
Implementation of 1-D std::vector like structure. 
 
size_t getProcessingUnits()
Get the total number of processors. 
 
bool allGather(T &send, openfpm::vector< T, Mem, gr > &v)
Gather the data from all processors. 
 
Class for cpu time benchmarking. 
 
void stop()
Stop the timer.