File: BitMapCarousel.h

package info (click to toggle)
combblas 2.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 190,476 kB
  • sloc: cpp: 55,912; ansic: 25,134; sh: 3,691; makefile: 548; csh: 66; python: 49; perl: 21
file content (192 lines) | stat: -rw-r--r-- 6,244 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
78
79
80
81
82
83
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/****************************************************************/
/* Parallel Combinatorial BLAS Library (for Graph Computations) */
/* version 1.4 -------------------------------------------------*/
/* date: 1/17/2014 ---------------------------------------------*/
/* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
/* this file contributed by Scott Beamer of UC Berkeley --------*/
/****************************************************************/
/*
 Copyright (c) 2010-2014, The Regents of the University of California
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */


#ifndef BITMAPCAROUSEL_H
#define BITMAPCAROUSEL_H

// #include <algorithm>
#include "BitMap.h"
#include "BitMapFringe.h"
#include "CommGrid.h"

namespace combblas {

template <class IT, class NT>
class BitMapCarousel {
 public:
  BitMapCarousel(std::shared_ptr<CommGrid> grid, IT glen, int local_subword_disp) {
    commGrid.reset(new CommGrid(*grid));   
    rotation_index = 0;
    global_size = glen;
    my_procrow = commGrid->GetRankInProcCol();
    my_proccol = commGrid->GetRankInProcRow();
    procrows = commGrid->GetGridRows();
    proccols = commGrid->GetGridCols();
	local_size = SizeOfChunk();
    rotlenuntil = RotLengthUntil();
    IT biggest_size = global_size - RotLengthUntil(procrows-1, proccols-1) + 63;
    bm = new BitMap(biggest_size);
    recv_buff = new BitMap(biggest_size);
    old_bm = new BitMap(biggest_size);
    sub_disps = new int[proccols];
    sub_disps[my_proccol] = local_subword_disp;	  
	MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT, sub_disps, 1, MPI_INT, commGrid->GetRowWorld());
    curr_subword_disp = local_subword_disp;
  }
  
  ~BitMapCarousel() {
    delete bm;
    delete recv_buff;
    delete old_bm;
    delete[] sub_disps;
  }

  // Return the global index of the start of the local chunk (inclusive)
  IT GetGlobalStartOfLocal() const {
    return rotlenuntil;
  }

  IT GetGlobalStartOfLocal(int col) const {
    return RotLengthUntil(my_procrow,col);
  }

  // Return the global index of the end of the local chunk (exclusive)
  IT GetGlobalEndOfLocal() const {
    return rotlenuntil + local_size;
  }

  IT GetGlobalEndOfLocal(int col) const {
    return GetGlobalStartOfLocal(col) + local_size;
  }

  bool GetBit(IT index) const {
    return bm->get_bit(index - rotlenuntil + curr_subword_disp);
  }

  void SetBit(IT index) {
    bm->set_bit(index - rotlenuntil + curr_subword_disp);
  }

  template <class NT1>
  void LoadVec(FullyDistVec<IT,NT1> & x) {
    bm->reset();
    local_size = x.LocArrSize();
    for (DenseVectorLocalIterator<IT,NT> it(x); it.HasNext(); it.Next())
      if (it.GetValue() != -1)
        bm->set_bit(it.GetLocIndex() + curr_subword_disp);
  }

  IT RotLengthUntil() {
    int curr_col = (my_proccol + rotation_index) % proccols;
    return RotLengthUntil(my_procrow, curr_col);
  }

  IT RotLengthUntil(int my_procrow, int my_proccol) const {
  	IT n_perprocrow = global_size / procrows;
  	IT n_thisrow;
  	if(my_procrow == procrows-1)
  		n_thisrow = global_size - (n_perprocrow*(procrows-1));
  	else
  		n_thisrow = n_perprocrow;	
  	IT n_perproc = n_thisrow / proccols;
  	return ((n_perprocrow * my_procrow)+(n_perproc*my_proccol));
  }

  IT SizeOfChunk() const {
    int curr_col = (my_proccol + rotation_index) % proccols;
    return SizeOfChunk(my_procrow, curr_col);
  }

  IT SizeOfChunk(int my_procrow, int my_proccol) const {
    IT my_upper_limit;
    if (my_proccol == (proccols-1)) {
      if (my_procrow == (procrows-1)) {
        my_upper_limit = global_size;
      }else {
        my_upper_limit = RotLengthUntil(my_procrow+1, 0);
      }
    } else {
      my_upper_limit = RotLengthUntil(my_procrow, (my_proccol+1) % proccols);
    }
    return my_upper_limit - RotLengthUntil(my_procrow,my_proccol);
  }

  void RotateAlongRow() {
    int source = (my_proccol+1) % proccols;
    int dest = (my_proccol+(proccols-1)) % proccols;
    rotation_index = (rotation_index + 1) % proccols;
    long send_words = (local_size + 63 + curr_subword_disp)>>6;
    long recv_words = (SizeOfChunk() + 63 + sub_disps[(my_proccol+rotation_index)%proccols])>>6;

    MPI_Comm RowWorld = commGrid->GetRowWorld();
	MPI_Status status;
	MPI_Sendrecv(bm->data(), send_words, MPIType<uint64_t>(), dest, ROTATE,
				   recv_buff->data(), recv_words, MPIType<uint64_t>(), source, ROTATE, RowWorld, &status);

    local_size = SizeOfChunk();
    rotlenuntil = RotLengthUntil();
    std::swap(bm, recv_buff);
    curr_subword_disp = sub_disps[(my_proccol+rotation_index)%proccols];
  }

  void UpdateFringe(BitMapFringe<IT,NT> &bm_fringe) {
    uint64_t* dest = bm_fringe.AccessBM()->data();
    uint64_t* curr = bm->data();
    uint64_t* old = old_bm->data();
    IT num_words = (local_size + 63 + curr_subword_disp) / 64;
    for (IT i=0; i<num_words; i++) {
      dest[i] = curr[i] ^ old[i];
    }
  }

  void SaveOld() {
    old_bm->copy_from(bm);
  }

private:
  std::shared_ptr<CommGrid> commGrid;
  int rotation_index;
  int my_procrow;
  int my_proccol;
  int procrows;
  int proccols;
  int curr_subword_disp;
  IT rotlenuntil;
  IT global_size;
  IT local_size;
  BitMap* bm;
  BitMap* recv_buff;
  BitMap* old_bm;
  int* sub_disps;
};

}

#endif // BITMAPCAROUSEL_H