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::vector<neighbor_tuple>& neighbors, std::string lookup_name)
7
{
8 9 10 11 12 13 14 15
    for(size_t s=0; s<neighbors.size(); s++) {
         std::regex re(neighbors[s]->regex_string);
          if(std::regex_match(lookup_name,re))
          {
            return neighbors[s]->name;
          }
       }
     assert(0);
16 17 18

}
                                                                            
19 20 21 22
NearestNeighborSWMUserCode::NearestNeighborSWMUserCode(
    boost::property_tree::ptree cfg,
    void**& generic_ptrs
) :
23
    process_cnt(cfg.get<uint32_t>("jobs.size", 1)),
24 25 26 27 28 29 30 31 32 33
    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))
34
{
35
    process_id = *((int*)generic_ptrs[0]);
36 37 38 39 40 41 42 43 44 45
    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];
    }
    std::cout << "dim_product is " << dim_product << " and process_cnt is " << process_cnt << std::endl;
    assert(dim_product == process_cnt);

46 47
    req_rt = AUTOMATIC;
    rsp_rt = AUTOMATIC;
48 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

}

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;

78
    
79 80 81 82 83
       std::cout << "xlat_coords_to_pid on coords ";
    for(size_t coords_idx=0; coords_idx<coords.size(); coords_idx++) {
    std::cout << " " << coords[coords_idx];
    }
    std::cout << endl;
84
    
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

    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);
169 170
        std::string regexed_string = GetFirstMatch(neighbors, neighbor_string);
        std::cout << "neighbor_string is " << neighbor_string << ", regexd_string is " << regexed_string << std::endl;
171

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

        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);
184 185
            std::string regexed_string = GetFirstMatch(neighbors, neighbor_string);
            std::cout << "neighbor_string is " << neighbor_string << ", regexd_string is " << regexed_string << std::endl;
186

187
            neighbors.push_back( std::make_tuple(neighbor_pid,regexed_string) );
188 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
        }
        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()
{

246
    
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    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);
            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;
        }
    }
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

    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();
293 294
    uint32_t pkt_rsp_bytes = 0;
    
295 296 297 298 299 300
    for(uint32_t iter=0; iter<iteration_cnt; iter++)
    {

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

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

309
            //msg_traffic_desc msg_desc;
310

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

            if(synchronous)
            {

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

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

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

            }
            else
            {

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

375 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 420
            }

        }

        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();
    assert(0);

}