Alpine3D

Alpine3D Svn Source Tree

Root/trunk/alpine3d/OMPControl.cc

1#include <alpine3d/OMPControl.h>
2#include <alpine3d/SnowpackInterfaceWorker.h>
3
4
5namespace OMPControl
6{
7 void getArraySliceParams(const size_t& dimx, const size_t& nbworkers, const size_t& idx_wk, size_t& startx_sub, size_t& nx_sub)
8 {
9 if (nbworkers < dimx) {
10 const size_t nx_avg = static_cast<size_t>( floor(static_cast<double>(dimx) / static_cast<double>(nbworkers)) );
11 const size_t remainder = dimx % nbworkers;
12
13 if (idx_wk < remainder) { //distribute remainder, 1 extra column per worker, on first workers
14 startx_sub = idx_wk * nx_avg + idx_wk;
15 nx_sub = nx_avg + 1;
16 } else { //all remainder has been distributed, we now attribute a normal number of columns
17 startx_sub = idx_wk * nx_avg + remainder;
18 nx_sub = nx_avg;
19 }
20 } else {
21 if (idx_wk < dimx) {
22 startx_sub = idx_wk;
23 nx_sub = 1;
24 } else {
25 nx_sub = 0;
26 startx_sub = 0;
27 }
28 }
29 }
30 void getArraySliceParamsOptim(const size_t& nbworkers, const std::vector<SnowStation*>& snow_station, const mio::DEMObject& dem,
31 const mio::Grid2DObject& landuse, std::vector<std::vector<size_t> >& omp_snow_stations_ind)
32 {
33 size_t dimx = dem.getNx();
34 size_t dimy = dem.getNy();
35
36 size_t n_skip_cell=0;
37 size_t n_skip_cell_2=0;
38
39 for (size_t ix = 0; ix < dimx; ix++) {
40 for (size_t iy = 0; iy < dimy; iy++) {
41 if (SnowpackInterfaceWorker::skipThisCell(landuse(ix,iy), dem(ix,iy)))
42 {n_skip_cell++;}
43 }
44 }
45 for(size_t i=0;i<snow_station.size();++i)
46 {
47 if(snow_station.at(i)==NULL)
48 {n_skip_cell_2++;}
49 }
50
51 size_t n_snow_station_compute = (snow_station.size()-n_skip_cell);
52 size_t n_snow_station_worker=n_snow_station_compute/nbworkers;
53 size_t remainders = n_snow_station_compute%nbworkers;
54
55 omp_snow_stations_ind.resize(nbworkers);
56
57 size_t worker_i=0;
58 size_t worker_n=0;
59 size_t remain = remainders>0?1:0;
60
61 for(size_t i=0;i<snow_station.size();++i)
62 {
63 omp_snow_stations_ind.at(worker_i).push_back(i);
64 if(snow_station.at(i)!=NULL)
65 {
66 worker_n++;
67 }
68 if(worker_n == n_snow_station_worker+remain)
69 {
70 remainders=remainders>0?remainders-1:0;
71 remain=remainders>0?1:0;
72 worker_i=worker_i<(nbworkers-1)?worker_i+1:worker_i;
73 worker_n=0;
74 }
75 }
76 }
77}

Archive Download this file

Revision: HEAD