Source

exafmm / gpu / include / b40c / util / cta_work_progress.cuh

Full commit
  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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
/******************************************************************************
 * 
 * Copyright 2010-2012 Duane Merrill
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 * 
 * For more information, see our Google Code project site: 
 * http://code.google.com/p/back40computing/
 * 
 * Thanks!
 * 
 ******************************************************************************/

/******************************************************************************
 * Management of temporary device storage needed for implementing work-stealing
 * progress between CTAs in a single grid
 ******************************************************************************/

#pragma once

#include <b40c/util/error_utils.cuh>
#include <b40c/util/cuda_properties.cuh>
#include <b40c/util/device_intrinsics.cuh>
#include <b40c/util/io/modified_load.cuh>
#include <b40c/util/io/modified_store.cuh>

namespace b40c {
namespace util {

/**
 * Manages device storage needed for implementing work-stealing
 * and queuing progress between CTAs in a single grid.
 *
 * Can be used for:
 *
 * (1) Work-stealing. Consists of a pair of counters in
 *     global device memory, optionally, a host-managed selector for
 *     indexing into the pair.
 *
 * (2) Device-managed queue.  Consists of a quadruplet of counters
 *     in global device memory and selection into the counters is made
 *     based upon the supplied iteration count.
 *     		Current iteration: incoming queue length
 *     		Next iteration: outgoing queue length
 *     		Next next iteration: needs to be reset before next iteration
 *     Can be used with work-stealing counters to for work-stealing
 *     queue operation
 *
 * For work-stealing passes, the current counter provides an atomic
 * reference of progress, and the current pass will typically reset
 * the counter for the next.
 *
 */
class CtaWorkProgress
{
protected :

	enum {
		QUEUE_COUNTERS 		= 4,
		STEAL_COUNTERS 		= 2,
		OVERFLOW_COUNTERS 	= 1,
	};

	// Seven pointer-sized counters in global device memory (we may not use
	// all of them, or may only use 32-bit versions of them)
	size_t *d_counters;

	// Host-controlled selector for indexing into d_counters.
	int progress_selector;

public:

	enum {
		COUNTERS = QUEUE_COUNTERS + STEAL_COUNTERS + OVERFLOW_COUNTERS
	};

	/**
	 * Constructor
	 */
	CtaWorkProgress() :
		d_counters(NULL),
		progress_selector(0) {}

	/**
	 * Resets all counters.  Must be called by thread-0 through
	 * thread-(COUNTERS - 1)
	 */
	template <typename SizeT>
	__device__ __forceinline__ void Reset()
	{
		SizeT reset_val = 0;
		util::io::ModifiedStore<util::io::st::cg>::St(
			reset_val, ((SizeT *) d_counters) + threadIdx.x);
	}

	//---------------------------------------------------------------------
	// Work-stealing
	//---------------------------------------------------------------------

	/**
	 * Steals work from the host-indexed progress counter, returning
	 * the offset of that work (from zero) and incrementing it by count.
	 * Typically called by thread-0
	 */
	template <typename SizeT>
	__device__ __forceinline__ SizeT Steal(int count)
	{
		SizeT* d_steal_counters = ((SizeT*) d_counters) + QUEUE_COUNTERS;
		return util::AtomicInt<SizeT>::Add(d_steal_counters + progress_selector, count);
	}

	/**
	 * Steals work from the specified iteration's progress counter, returning the
	 * offset of that work (from zero) and incrementing it by count.
	 * Typically called by thread-0
	 */
	template <typename SizeT, typename IterationT>
	__device__ __forceinline__ SizeT Steal(int count, IterationT iteration)
	{
		SizeT* d_steal_counters = ((SizeT*) d_counters) + QUEUE_COUNTERS;
		return util::AtomicInt<SizeT>::Add(d_steal_counters + (iteration & 1), count);
	}

	/**
	 * Resets the work progress for the next host-indexed work-stealing
	 * pass.  Typically called by thread-0 in block-0.
	 */
	template <typename SizeT>
	__device__ __forceinline__ void PrepResetSteal()
	{
		SizeT 	reset_val = 0;
		SizeT* 	d_steal_counters = ((SizeT*) d_counters) + QUEUE_COUNTERS;
		util::io::ModifiedStore<util::io::st::cg>::St(
				reset_val, d_steal_counters + (progress_selector ^ 1));
	}

	/**
	 * Resets the work progress for the specified work-stealing iteration.
	 * Typically called by thread-0 in block-0.
	 */
	template <typename SizeT, typename IterationT>
	__device__ __forceinline__ void PrepResetSteal(IterationT iteration)
	{
		SizeT 	reset_val = 0;
		SizeT* 	d_steal_counters = ((SizeT*) d_counters) + QUEUE_COUNTERS;
		util::io::ModifiedStore<util::io::st::cg>::St(
			reset_val, d_steal_counters + (iteration & 1));
	}


	//---------------------------------------------------------------------
	// Queuing
	//---------------------------------------------------------------------

	/**
	 * Get counter for specified iteration
	 */
	template <typename SizeT, typename IterationT>
	__device__ __forceinline__ SizeT* GetQueueCounter(IterationT iteration)
	{
		return ((SizeT*) d_counters) + (iteration & 3);
	}

	/**
	 * Load work queue length for specified iteration
	 */
	template <typename SizeT, typename IterationT>
	__device__ __forceinline__ SizeT LoadQueueLength(IterationT iteration)
	{
		SizeT queue_length;
		util::io::ModifiedLoad<util::io::ld::cg>::Ld(
			queue_length, GetQueueCounter<SizeT>(iteration));
		return queue_length;
	}

	/**
	 * Store work queue length for specified iteration
	 */
	template <typename SizeT, typename IterationT>
	__device__ __forceinline__ void StoreQueueLength(SizeT queue_length, IterationT iteration)
	{
		util::io::ModifiedStore<util::io::st::cg>::St(
			queue_length, GetQueueCounter<SizeT>(iteration));
	}

	/**
	 * Enqueues work from the specified iteration's queue counter, returning the
	 * offset of that work (from zero) and incrementing it by count.
	 * Typically called by thread-0
	 */
	template <typename SizeT, typename IterationT>
	__device__ __forceinline__ SizeT Enqueue(SizeT count, IterationT iteration)
	{
		return util::AtomicInt<SizeT>::Add(
			GetQueueCounter<SizeT>(iteration),
			count);
	}

	/**
	 * Sets the overflow counter to non-zero
	 */
	template <typename SizeT>
	__device__ __forceinline__ void SetOverflow ()
	{
		((SizeT*) d_counters)[QUEUE_COUNTERS + STEAL_COUNTERS] = 1;
	}

};


/**
 * Version of work progress with storage lifetime management.
 *
 * We can use this in host enactors, and pass the base CtaWorkProgress
 * as parameters to kernels.
 */
class CtaWorkProgressLifetime : public CtaWorkProgress
{
protected:

	// GPU d_counters was allocated on
	int gpu;

public:

	/**
	 * Constructor
	 */
	CtaWorkProgressLifetime() :
		CtaWorkProgress(),
		gpu(CUB_INVALID_DEVICE) {}


	/**
	 * Deallocates and resets the progress counters
	 */
	cudaError_t HostReset()
	{
		cudaError_t retval = cudaSuccess;

		do {
			if (gpu != CUB_INVALID_DEVICE) {

				// Save current gpu
				int current_gpu;
				if (retval = util::B40CPerror(cudaGetDevice(&current_gpu),
					"CtaWorkProgress cudaGetDevice failed: ", __FILE__, __LINE__)) break;

				// Deallocate
				if (retval = util::B40CPerror(cudaSetDevice(gpu),
					"CtaWorkProgress cudaSetDevice failed: ", __FILE__, __LINE__)) break;
				if (retval = util::B40CPerror(cudaFree(d_counters),
					"CtaWorkProgress cudaFree d_counters failed: ", __FILE__, __LINE__)) break;

				d_counters = NULL;
				gpu = CUB_INVALID_DEVICE;

				// Restore current gpu
				if (retval = util::B40CPerror(cudaSetDevice(current_gpu),
					"CtaWorkProgress cudaSetDevice failed: ", __FILE__, __LINE__)) break;
			}

			progress_selector = 0;

		} while (0);

		return retval;
	}


	/**
	 * Destructor
	 */
	virtual ~CtaWorkProgressLifetime()
	{
		HostReset();
	}


	/**
	 * Sets up the progress counters for the next kernel launch (lazily
	 * allocating and initializing them if necessary)
	 */
	cudaError_t Setup()
	{
		cudaError_t retval = cudaSuccess;
		do {

			// Make sure that our progress counters are allocated
			if (!d_counters) {

				size_t h_counters[COUNTERS];
				for (int i = 0; i < COUNTERS; i++) {
					h_counters[i] = 0;
				}

				// Allocate and initialize
				if (retval = util::B40CPerror(cudaGetDevice(&gpu),
					"CtaWorkProgress cudaGetDevice failed: ", __FILE__, __LINE__)) break;
				if (retval = util::B40CPerror(cudaMalloc((void**) &d_counters, sizeof(h_counters)),
					"CtaWorkProgress cudaMalloc d_counters failed", __FILE__, __LINE__)) break;
				if (retval = util::B40CPerror(cudaMemcpy(d_counters, h_counters, sizeof(h_counters), cudaMemcpyHostToDevice),
					"CtaWorkProgress cudaMemcpy d_counters failed", __FILE__, __LINE__)) break;
			}

			// Update our progress counter selector to index the next progress counter
			progress_selector ^= 1;

		} while (0);

		return retval;
	}


	/**
	 * Checks if overflow counter is set
	 */
	template <typename SizeT>
	cudaError_t CheckOverflow(bool &overflow)	// out param
	{
		cudaError_t retval = cudaSuccess;

		do {
			SizeT counter;

			if (retval = util::B40CPerror(cudaMemcpy(
					&counter,
					((SizeT*) d_counters) + QUEUE_COUNTERS + STEAL_COUNTERS,
					1 * sizeof(SizeT),
					cudaMemcpyDeviceToHost),
				"CtaWorkProgress cudaMemcpy d_counters failed", __FILE__, __LINE__)) break;

			overflow = counter;

		} while (0);

		return retval;
	}


	/**
	 * Acquire work queue length
	 */
	template <typename IterationT, typename SizeT>
	cudaError_t GetQueueLength(
		IterationT iteration,
		SizeT &queue_length)		// out param
	{
		cudaError_t retval = cudaSuccess;

		do {
			int queue_length_idx = iteration & 0x3;

			if (retval = util::B40CPerror(cudaMemcpy(
					&queue_length,
					((SizeT*) d_counters) + queue_length_idx,
					1 * sizeof(SizeT),
					cudaMemcpyDeviceToHost),
				"CtaWorkProgress cudaMemcpy d_counters failed", __FILE__, __LINE__)) break;

		} while (0);

		return retval;
	}


	/**
	 * Set work queue length
	 */
	template <typename IterationT, typename SizeT>
	cudaError_t SetQueueLength(
		IterationT iteration,
		SizeT queue_length)
	{
		cudaError_t retval = cudaSuccess;

		do {
			int queue_length_idx = iteration & 0x3;

			if (retval = util::B40CPerror(cudaMemcpy(
					((SizeT*) d_counters) + queue_length_idx,
					&queue_length,
					1 * sizeof(SizeT),
					cudaMemcpyHostToDevice),
				"CtaWorkProgress cudaMemcpy d_counters failed", __FILE__, __LINE__)) break;

		} while (0);

		return retval;
	}
};

} // namespace util
} // namespace b40c