nearest_neighbor_swm_user_code.cpp 11.6 KB
Newer Older
1

2 3 4 5
#include "nearest_neighbor_swm_user_code.h"
#include "boost_ptree_array_to_std_vector.h"
extern uint64_t global_cycle;

6
std::string GetFirstMatch(std::string lookup_name)
7 8
{

9 10 11 12 13 14 15
    for(size_t s=0; s<msg_traffic_def_vector.size(); s++) {
        std::regex re(msg_traffic_def_vector[s]->regex_string);
        if(std::regex_match(lookup_name,re))
        {
        return msg_traffic_def_vector[s]->name;
        }
    }
16 17
    return "";
    //assert(0);
18 19
}
                                                                            
20 21 22 23
NearestNeighborSWMUserCode::NearestNeighborSWMUserCode(
    boost::property_tree::ptree cfg,
    void**& generic_ptrs
) :
24
    process_cnt(cfg.get<uint32_t>("jobs.size", 1)),
25 26 27 28 29 30 31 32 33 34
    iteration_cnt(cfg.get<uint32_t>("jobs.cfg.iteration_cnt", 1)),
    noop_cnt(cfg.get<uint32_t>("jobs.cfg.noop_cnt", 1)),
    compute_delay(cfg.get<uint32_t>("jobs.cfg.compute_delay", 1)),
    dimension_cnt(cfg.get<uint32_t>("jobs.cfg.dimension_cnt",0)),
    msg_size(cfg.get<uint32_t>("jobs.cfg.msg_size", 0)),
    dimension_sizes(boost_ptree_array_to_std_vector<uint32_t>(cfg,"jobs.cfg.dimension_sizes", {0})),
                max_dimension_distance(cfg.get<uint32_t>("jobs.cfg.max_dimension_distance",0)),
                synchronous(cfg.get<bool>("jobs.cfg.synchronous",false)),
                iterations_per_sync(cfg.get<uint32_t>("jobs.cfg.iterations_per_sync",1)),
                randomize_communication_order(cfg.get<bool>("jobs.cfg.randomize_communication_order",false))
35
{
36
    process_id = *((int*)generic_ptrs[0]);
37 38 39 40 41 42 43
    assert(dimension_sizes.size() == dimension_cnt);

    size_t dim_product = 1;
    for(size_t dim_i = 0; dim_i < dimension_sizes.size(); dim_i++)
    {
        dim_product *= dimension_sizes[dim_i];
    }
44
    //std::cout << "dim_product is " << dim_product << " and process_cnt is " << process_cnt << std::endl;
45 46
    assert(dim_product == process_cnt);

47 48
    req_rt = AUTOMATIC;
    rsp_rt = AUTOMATIC;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

}

void
NearestNeighborSWMUserCode::xlat_pid_to_coords(
    uint32_t pid,
    std::vector<uint32_t>& coords
)
{

    coords.clear();

    uint32_t dim_div = 1;
    for(uint32_t dim_idx=0; dim_idx<dimension_cnt; dim_idx++)
    {
        uint32_t pid_coord_in_dim = (pid / dim_div) % dimension_sizes[dim_idx];
        dim_div *= dimension_sizes[dim_idx];
        coords.push_back(pid_coord_in_dim);
    }
}

void
NearestNeighborSWMUserCode::xlat_coords_to_pid(
    std::vector<uint32_t> coords,
    uint32_t& pid
)
{

    pid=0;

79
    
80
    /*std::cout << "xlat_coords_to_pid on coords ";
81 82 83 84
    for(size_t coords_idx=0; coords_idx<coords.size(); coords_idx++) {
    std::cout << " " << coords[coords_idx];
    }
    std::cout << endl;
85
    */
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    uint32_t dim_mult = 1;
    for(uint32_t dim_idx=0; dim_idx<dimension_cnt; dim_idx++)
    {
        pid += coords[dim_idx] * dim_mult;
        dim_mult *= dimension_sizes[dim_idx];
    }
}

std::string
NearestNeighborSWMUserCode::get_neighbor_string(
    uint32_t my_pid,
    uint32_t neighbor_pid
)
{
    std::vector<uint32_t> my_coords;
    std::vector<uint32_t> neighbor_coords;

    xlat_pid_to_coords(my_pid, my_coords);
    xlat_pid_to_coords(neighbor_pid, neighbor_coords);

    assert(my_coords.size() == neighbor_coords.size());

    std::ostringstream oss;

    for(size_t c=0; c<my_coords.size(); c++)
    {

        if(my_coords[c] != neighbor_coords[c])
        {
            if(my_coords[c] == 0)
            {
                if(neighbor_coords[c] == (my_coords[c]+1))
                {
                    oss << "p" << c;
                }
                else if(neighbor_coords[c] == (dimension_sizes[c]-1))
                {
                    oss << "m" << c;
                }
                else
                {
                    assert(0);
                }
            }
            else
            {
                if(neighbor_coords[c] == ((my_coords[c] + 1) % dimension_sizes[c]))
                {
                    oss << "p" << c;
                }
                else if(neighbor_coords[c] == (my_coords[c] - 1))
                {
                    oss << "m" << c;
                }
                else
                {
                    assert(0);
                }
            }
        }
    }

    return oss.str();
}

void
NearestNeighborSWMUserCode::derive_neighbors_recurse(
    std::vector<uint32_t> coords,
    std::vector<neighbor_tuple>& neighbors,
    uint32_t dimension_to_vary,
    uint32_t accumulated_dimension_distance
)
{
    std::vector<uint32_t> coords_copy;
    //uint32_t accumulated_dimension_distance_copy;
    coords_copy.resize(coords.size());

    if(accumulated_dimension_distance == max_dimension_distance)
    {
        uint32_t neighbor_pid;
        xlat_coords_to_pid(coords, neighbor_pid);

        std::string neighbor_string = get_neighbor_string(process_id, neighbor_pid);
170
        std::string regexed_string = GetFirstMatch(neighbor_string);
171
        //std::cout << "neighbor_string is " << neighbor_string << ", regexd_string is " << regexed_string << std::endl;
172

173
        neighbors.push_back( std::make_tuple(neighbor_pid,regexed_string) );
174 175 176 177 178 179 180 181 182 183 184

        return;
    }
    else if(dimension_to_vary == dimension_cnt)
    {
        if(accumulated_dimension_distance > 0)
        {
            uint32_t neighbor_pid;
            xlat_coords_to_pid(coords, neighbor_pid);

            std::string neighbor_string = get_neighbor_string(process_id, neighbor_pid);
185
            std::string regexed_string = GetFirstMatch(neighbor_string);
186
            //std::cout << "neighbor_string is " << neighbor_string << ", regexd_string is " << regexed_string << std::endl;
187

188
            neighbors.push_back( std::make_tuple(neighbor_pid,regexed_string) );
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        }
        return;
    }

    //negative
    coords_copy = coords;
    if(coords_copy[dimension_to_vary] == 0)
    {
        coords_copy[dimension_to_vary] = (dimension_sizes[dimension_to_vary] -1);
    }
    else
    {
        coords_copy[dimension_to_vary] = (coords_copy[dimension_to_vary] -1);
    }

    derive_neighbors_recurse(
        coords_copy,
        neighbors,
        dimension_to_vary+1,
        accumulated_dimension_distance+1
    );


    //none
    coords_copy = coords;

    derive_neighbors_recurse(
        coords_copy,
        neighbors,
        dimension_to_vary+1,
        accumulated_dimension_distance
    );


    //positive
    coords_copy = coords;
    if(coords_copy[dimension_to_vary] == (dimension_sizes[dimension_to_vary] -1))
    {
        coords_copy[dimension_to_vary] = 0;
    }
    else
    {
        coords_copy[dimension_to_vary] = (coords_copy[dimension_to_vary] +1);
    }

    derive_neighbors_recurse(
        coords_copy,
        neighbors,
        dimension_to_vary+1,
        accumulated_dimension_distance+1
    );

}

void
NearestNeighborSWMUserCode::call()
{

247
    
248 249 250 251 252 253 254
    if(process_id == 0) {   //lets print every pid in coords and back again
    std::vector<uint32_t> coords;
        uint32_t pid_again;
        for(uint32_t pid=0; pid<process_cnt; pid++) {
            coords.clear();
            pid_again=0;
            xlat_pid_to_coords(pid, coords);
255 256 257 258 259 260 261
//            std::cout << "pid " << pid << " has coords.size " << coords.size() << " ";
//            for(size_t i=0; i<coords.size(); i++) {
//            std::cout << " " << coords[i];
//            }
//            std::cout << "; which have pid ";
//            xlat_coords_to_pid(coords, pid_again);
//            std::cout << pid_again << endl;
262 263
        }
    }
264
    
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

    std::vector<uint32_t> my_coords;
    std::vector<uint32_t> neighbor_pids;
    xlat_pid_to_coords(process_id, my_coords);

    derive_neighbors_recurse(my_coords, neighbors);

    /*
    if(process_id == 0)
    {
        std::cout << "neighbors of pid " << process_id << " are: ";
        for(size_t neighbors_idx=0; neighbors_idx<neighbors.size(); neighbors_idx++) {
            std::cout << " " << std::get<0>(neighbors[neighbors_idx]) << "," << std::get<1>(neighbors[neighbors_idx]);
        }
        std::cout << "\n";
    }
    */

    uint32_t* send_handles = NULL;
    uint32_t* recv_handles = NULL;

    if(synchronous)
    {
        send_handles = new uint32_t[neighbors.size()*iterations_per_sync];
        recv_handles = new uint32_t[neighbors.size()*iterations_per_sync];
    }

    uint32_t iter_before_sync = 0;
    uint32_t neighbors_size=neighbors.size();
294
    uint32_t pkt_rsp_bytes = 0;
295
   
296 297 298 299 300 301
    for(uint32_t iter=0; iter<iteration_cnt; iter++)
    {

        //shuffle the neighbors
        if(randomize_communication_order)
        {
302 303
//            auto rng = std::default_random_engine {};
            std::random_shuffle(neighbors.begin(), neighbors.end());
304 305 306 307 308 309
        }

        //send to each neighbor
        for(size_t neighbor_idx=0; neighbor_idx<neighbors.size(); neighbor_idx++)
        {

310
            //msg_traffic_desc msg_desc;
311

312
            //GetMsgDetails(&msg_desc, std::get<1>(neighbors[neighbor_idx]));
313 314 315 316 317 318 319

            if(synchronous)
            {

                //send/recv pair that we'll later wait on

                SWM_Isend(
320
                    std::get<0>(neighbors[neighbor_idx]),
321 322
                    SWM_COMM_WORLD,
                    process_id,
323 324
                    0, // MM additions
                    1, // MM additions
325
                    NO_BUFFER,
326 327
                    msg_size, //msg_desc.msg_req_bytes,
                    pkt_rsp_bytes, //msg_desc.pkt_rsp_bytes,
328
                    &(send_handles[neighbor_idx+iter_before_sync*neighbors_size]),
329 330
                    0,
                    0
331 332 333
                );

                SWM_Irecv(
334
                    std::get<0>(neighbors[neighbor_idx]),
335
                    SWM_COMM_WORLD,
336
                    std::get<0>(neighbors[neighbor_idx]),
337 338 339 340 341
                    NO_BUFFER,
                    &(recv_handles[neighbor_idx+iter_before_sync*neighbors_size])
                );
                for(uint32_t noop=0; noop<noop_cnt; noop++)
                {
342 343
		  assert(0); // Does CODES have an equivalent of a NOOP?
		  //SWM_Noop();
344 345 346 347 348 349 350
                }

            }
            else
            {

                //fire and forget
351 352 353
	      // purposes of the paper lets just use Isend/Irecv
	      assert(0);
	      /*
354 355
                SWM_Synthetic(
                    std::get<0>(neighbors[neighbor_idx]),  //dst
356 357 358
                    0,
                    0,
                    0,
359 360 361 362 363 364
                    msg_size, //msg_desc.msg_req_bytes,
                    0, //msg_desc.msg_rsp_bytes,
                    pkt_rsp_bytes, //msg_desc.pkt_rsp_bytes,
                    req_rt, //msg_desc.msg_req_routing_type,
                    rsp_rt, //msg_desc.msg_rsp_routing_type,
                    rsp_rt, //msg_desc.pkt_rsp_routing_type,
365
                    NULL,
366
                    NULL, //msg_desc.attribute
367 368 369 370 371 372 373 374 375
#ifdef FABSIM_EMULATION
                    , msg_desc.l2_encoding
#endif
                );
                for(uint32_t noop=0; noop<noop_cnt; noop++)
                {
                    SWM_Noop();
                }

376 377
		*/

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
            }

        }

        if(synchronous)
        {


            iter_before_sync++;
            if(iter_before_sync == iterations_per_sync || iter == iteration_cnt-1 )
            {
                //std::cout << "begin wait at time: " << global_cycle << std::endl;
                SWM_Waitall(
                    neighbors.size()*iter_before_sync,
                    send_handles
                );

                SWM_Waitall(
                    neighbors.size()*iter_before_sync,
                    recv_handles
                );
                iter_before_sync = 0;
                //std::cout << "end wait at time: " << global_cycle << std::endl;
            }


        }
        else
        {

            if (compute_delay)
            {
                SWM_Compute(compute_delay);
            }

        }

    }

    SWM_Finalize();
}