Actual source code: sellhip.hip.cxx
1: #include "hip/hip_runtime.h"
2: #include <hip/hip_runtime.h>
4: #include <petscdevice_hip.h>
5: #include <petsc/private/cupmatomics.hpp>
6: #include <petsc/private/deviceimpl.h>
7: #include <../src/mat/impls/sell/seq/sell.h>
9: #if PetscDefined(HAVE_HIPCUDA)
10: #define WARP_SIZE 32
11: #else
12: #define WARP_SIZE 64
13: #endif
15: typedef struct {
16: PetscInt maxallocmat;
17: PetscInt totalentries;
18: PetscInt *colidx; /* column index array, device pointer */
19: MatScalar *val; /* value array, device pointer */
20: PetscInt totalslices;
21: PetscInt *sliidx; /* slice index array, device pointer */
22: PetscObjectState nonzerostate;
23: PetscInt kernelchoice;
24: PetscInt blocky;
25: PetscInt chunksperblock;
26: PetscInt totalchunks;
27: PetscInt *chunk_slice_map; /* starting slice for each chunk, device pointer */
28: } Mat_SeqSELLHIP;
30: static PetscErrorCode MatSeqSELLHIPGetContext(PetscDeviceContext *dctx, hipStream_t *stream)
31: {
32: void *handle;
34: PetscFunctionBegin;
35: PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(dctx, PETSC_DEVICE_HIP));
36: PetscCall(PetscDeviceContextGetStreamHandle(*dctx, &handle));
37: *stream = *(hipStream_t *)handle;
38: PetscFunctionReturn(PETSC_SUCCESS);
39: }
41: static PetscErrorCode MatSeqSELLHIP_Destroy(Mat_SeqSELLHIP **hipstruct)
42: {
43: PetscFunctionBegin;
44: if (*hipstruct) {
45: PetscCallHIP(hipFree((*hipstruct)->colidx));
46: PetscCallHIP(hipFree((*hipstruct)->val));
47: PetscCallHIP(hipFree((*hipstruct)->sliidx));
48: PetscCallHIP(hipFree((*hipstruct)->chunk_slice_map));
49: PetscCall(PetscFree(*hipstruct));
50: }
51: PetscFunctionReturn(PETSC_SUCCESS);
52: }
54: static PetscErrorCode MatSeqSELLHIPCopyToGPU(Mat A)
55: {
56: Mat_SeqSELLHIP *hipstruct = (Mat_SeqSELLHIP *)A->spptr;
57: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
58: PetscDeviceContext dctx;
59: hipStream_t stream;
61: PetscFunctionBegin;
62: PetscCall(MatSeqSELLHIPGetContext(&dctx, &stream));
63: if (A->offloadmask == PETSC_OFFLOAD_UNALLOCATED || A->offloadmask == PETSC_OFFLOAD_CPU) {
64: PetscCall(PetscLogEventBegin(MAT_HIPCopyToGPU, A, 0, 0, 0));
65: if (hipstruct->sliidx && A->nonzerostate == hipstruct->nonzerostate) {
66: /* copy values only */
67: PetscCallHIP(hipMemcpyAsync(hipstruct->val, a->val, a->sliidx[a->totalslices] * sizeof(MatScalar), hipMemcpyHostToDevice, stream));
68: PetscCall(PetscLogCpuToGpu(a->sliidx[a->totalslices] * (sizeof(MatScalar))));
69: } else {
70: PetscCallHIP(hipFree(hipstruct->colidx));
71: PetscCallHIP(hipFree(hipstruct->val));
72: PetscCallHIP(hipFree(hipstruct->sliidx));
73: PetscCallHIP(hipFree(hipstruct->chunk_slice_map));
74: hipstruct->maxallocmat = a->maxallocmat;
75: hipstruct->totalentries = a->sliidx[a->totalslices];
76: hipstruct->totalslices = a->totalslices;
77: hipstruct->totalchunks = a->totalchunks;
78: PetscCallHIP(hipMalloc((void **)&hipstruct->colidx, a->maxallocmat * sizeof(*hipstruct->colidx)));
79: PetscCallHIP(hipMalloc((void **)&hipstruct->val, a->maxallocmat * sizeof(*hipstruct->val)));
80: /* copy values, nz or maxallocmat? */
81: PetscCallHIP(hipMemcpyAsync(hipstruct->colidx, a->colidx, a->sliidx[a->totalslices] * sizeof(*a->colidx), hipMemcpyHostToDevice, stream));
82: PetscCallHIP(hipMemcpyAsync(hipstruct->val, a->val, a->sliidx[a->totalslices] * sizeof(*a->val), hipMemcpyHostToDevice, stream));
84: PetscCallHIP(hipMalloc((void **)&hipstruct->sliidx, (a->totalslices + 1) * sizeof(*hipstruct->sliidx)));
85: PetscCallHIP(hipMemcpyAsync(hipstruct->sliidx, a->sliidx, (a->totalslices + 1) * sizeof(*a->sliidx), hipMemcpyHostToDevice, stream));
86: PetscCallHIP(hipMalloc((void **)&hipstruct->chunk_slice_map, a->totalchunks * sizeof(*hipstruct->chunk_slice_map)));
87: PetscCallHIP(hipMemcpyAsync(hipstruct->chunk_slice_map, a->chunk_slice_map, a->totalchunks * sizeof(*a->chunk_slice_map), hipMemcpyHostToDevice, stream));
88: PetscCall(PetscLogCpuToGpu(a->sliidx[a->totalslices] * (sizeof(MatScalar) + sizeof(PetscInt)) + (a->totalslices + 1 + a->totalchunks) * sizeof(PetscInt)));
89: }
90: PetscCall(PetscDeviceContextSynchronize(dctx));
91: PetscCall(PetscLogEventEnd(MAT_HIPCopyToGPU, A, 0, 0, 0));
92: hipstruct->nonzerostate = A->nonzerostate;
93: A->offloadmask = PETSC_OFFLOAD_BOTH;
94: }
95: PetscFunctionReturn(PETSC_SUCCESS);
96: }
98: static __global__ void matmult_seqsell_basic_kernel(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
99: {
100: PetscInt i, row, slice_id, row_in_slice;
101: MatScalar sum;
102: /* one thread per row. */
103: row = blockIdx.x * blockDim.x + threadIdx.x;
104: if (row < nrows) {
105: slice_id = row / sliceheight;
106: row_in_slice = row % sliceheight;
107: sum = 0.0;
108: for (i = sliidx[slice_id] + row_in_slice; i < sliidx[slice_id + 1]; i += sliceheight) sum += aval[i] * x[acolidx[i]];
109: y[row] = sum;
110: }
111: }
113: static __global__ void matmultadd_seqsell_basic_kernel(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
114: {
115: PetscInt i, row, slice_id, row_in_slice;
116: MatScalar sum;
117: /* one thread per row. */
118: row = blockIdx.x * blockDim.x + threadIdx.x;
119: if (row < nrows) {
120: slice_id = row / sliceheight;
121: row_in_slice = row % sliceheight;
122: sum = 0.0;
123: for (i = sliidx[slice_id] + row_in_slice; i < sliidx[slice_id + 1]; i += sliceheight) sum += aval[i] * x[acolidx[i]];
124: z[row] = y[row] + sum;
125: }
126: }
128: #if !PetscDefined(USE_COMPLEX)
129: static __device__ __forceinline__ PetscScalar MatSeqSELLHIPShuffleDown(PetscScalar value, unsigned int delta, int width = WARP_SIZE)
130: {
131: #if PetscDefined(HAVE_HIPCUDA)
132: return __shfl_down_sync(0xffffffff, value, delta, width);
133: #else
134: return __shfl_down(value, delta, width);
135: #endif
136: }
138: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wpass-failed")
139: /* use 1 block per slice, suitable for large slice width */
140: template <int BLOCKY>
141: __global__ void matmult_seqsell_tiled_kernel9(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
142: {
143: __shared__ MatScalar shared[WARP_SIZE][BLOCKY];
144: PetscInt i, row, slice_id = blockIdx.x;
145: int tid = threadIdx.x + threadIdx.y * WARP_SIZE;
146: /* transposed index */
147: int tidx = tid % BLOCKY;
148: int tidy = tid / BLOCKY;
149: PetscScalar t = 0.0;
151: row = slice_id * sliceheight + threadIdx.x % sliceheight;
152: if (row < nrows) {
153: for (i = sliidx[slice_id] + threadIdx.x + WARP_SIZE * threadIdx.y; i < sliidx[slice_id + 1]; i += WARP_SIZE * BLOCKY) t += aval[i] * x[acolidx[i]];
154: }
155: #pragma unroll
156: for (int offset = WARP_SIZE / 2; offset >= sliceheight; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset);
157: /* transpose layout to reduce each row using warp shfl */
158: if (threadIdx.x < sliceheight) shared[threadIdx.x][threadIdx.y] = t;
159: __syncthreads();
160: if (tidy < sliceheight) t = shared[tidy][tidx];
161: #pragma unroll
162: for (int offset = BLOCKY / 2; offset > 0; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset, BLOCKY);
163: if (tidx == 0 && tidy < sliceheight) shared[0][tidy] = t;
164: __syncthreads();
165: if (row < nrows && threadIdx.y == 0 && threadIdx.x < sliceheight) y[row] = shared[0][threadIdx.x];
166: }
168: /* use 1 block per slice, suitable for large slice width */
169: template <int BLOCKY>
170: __global__ void matmultadd_seqsell_tiled_kernel9(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
171: {
172: __shared__ MatScalar shared[WARP_SIZE][BLOCKY];
173: PetscInt i, row, slice_id = blockIdx.x;
174: int tid = threadIdx.x + threadIdx.y * WARP_SIZE;
175: /* transposed index */
176: int tidx = tid % BLOCKY;
177: int tidy = tid / BLOCKY;
178: PetscScalar t = 0.0;
180: row = slice_id * sliceheight + threadIdx.x % sliceheight;
181: if (row < nrows) {
182: for (i = sliidx[slice_id] + threadIdx.x + WARP_SIZE * threadIdx.y; i < sliidx[slice_id + 1]; i += WARP_SIZE * BLOCKY) t += aval[i] * x[acolidx[i]];
183: }
184: #pragma unroll
185: for (int offset = WARP_SIZE / 2; offset >= sliceheight; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset);
186: /* transpose layout to reduce each row using warp shfl */
187: if (threadIdx.x < sliceheight) shared[threadIdx.x][threadIdx.y] = t;
188: __syncthreads();
189: if (tidy < sliceheight) t = shared[tidy][tidx];
190: #pragma unroll
191: for (int offset = BLOCKY / 2; offset > 0; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset, BLOCKY);
192: if (tidx == 0 && tidy < sliceheight) shared[0][tidy] = t;
193: __syncthreads();
194: if (row < nrows && threadIdx.y == 0 && threadIdx.x < sliceheight) z[row] = y[row] + shared[0][threadIdx.x];
195: }
197: template <int BLOCKY>
198: __device__ __forceinline__ static bool segment_scan(PetscInt flag[], MatScalar shared[], PetscScalar *val)
199: {
200: bool head = true;
201: #pragma unroll
202: for (int i = 1; i < BLOCKY * 2; i <<= 1) {
203: int halfwarpid = threadIdx.y * 2 + threadIdx.x / (WARP_SIZE / 2);
204: shared[threadIdx.x + threadIdx.y * WARP_SIZE] = 0;
205: if (halfwarpid >= i && flag[halfwarpid - i] == flag[halfwarpid]) {
206: shared[threadIdx.x + threadIdx.y * WARP_SIZE] = *val;
207: if (i == 1) head = false;
208: }
209: __syncthreads();
210: if (halfwarpid < BLOCKY * 2 - i) *val += shared[threadIdx.x + threadIdx.y * WARP_SIZE + i * WARP_SIZE];
211: __syncthreads();
212: }
213: return head;
214: }
216: /* load-balancing version. Chunksize is equal to the number of threads per block */
217: template <int BLOCKY>
218: __global__ void matmult_seqsell_tiled_kernel8(PetscInt nrows, PetscInt sliceheight, PetscInt chunksperblock, PetscInt totalchunks, const PetscInt *chunk_slice_map, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
219: {
220: __shared__ MatScalar shared[BLOCKY * WARP_SIZE];
221: PetscInt gid, row, start_slice, cid;
222: PetscScalar t = 0.0;
223: AtomicAdd<MatScalar> atomAdd;
224: /* zero out y */
225: for (int iter = 0; iter < 1 + (nrows - 1) / (gridDim.x * WARP_SIZE * BLOCKY); iter++) {
226: gid = gridDim.x * WARP_SIZE * BLOCKY * iter + blockIdx.x * BLOCKY * WARP_SIZE + threadIdx.y * WARP_SIZE + threadIdx.x;
227: if (gid < nrows) y[gid] = 0.0;
228: }
229: for (int iter = 0; iter < chunksperblock; iter++) {
230: cid = blockIdx.x * chunksperblock + iter; /* chunk id */
231: if (cid < totalchunks) {
232: start_slice = chunk_slice_map[cid]; /* starting slice at each iteration */
233: gid = cid * BLOCKY * WARP_SIZE + threadIdx.y * WARP_SIZE + threadIdx.x;
234: if ((cid + 1) * BLOCKY * WARP_SIZE > sliidx[start_slice + 1]) { /* this iteration covers more than one slice */
235: __shared__ PetscInt flag[BLOCKY * 2];
236: bool write;
237: PetscInt slice_id = start_slice, totalslices = PetscCeilIntMacro(nrows, sliceheight), totalentries = sliidx[totalslices];
238: /* find out the slice that this element belongs to */
239: while (gid < totalentries && gid >= sliidx[slice_id + 1]) slice_id++;
240: if (threadIdx.x % (WARP_SIZE / 2) == 0) flag[threadIdx.y * 2 + threadIdx.x / (WARP_SIZE / 2)] = slice_id;
241: row = slice_id * sliceheight + threadIdx.x % sliceheight;
242: if (row < nrows && gid < totalentries) t = aval[gid] * x[acolidx[gid]];
243: __syncthreads();
244: write = segment_scan<BLOCKY>(flag, shared, &t);
245: if (row < nrows && gid < totalentries && write) atomAdd(y[row], t);
246: t = 0.0;
247: } else { /* this iteration covers only one slice */
248: row = start_slice * sliceheight + threadIdx.x % sliceheight;
249: if (row < nrows) t += aval[gid] * x[acolidx[gid]];
250: if (iter == chunksperblock - 1 || (cid + 2) * BLOCKY * WARP_SIZE > sliidx[start_slice + 1]) { /* last iteration or next iteration covers more than one slice */
251: int tid = threadIdx.x + threadIdx.y * WARP_SIZE, tidx = tid % BLOCKY, tidy = tid / BLOCKY;
252: /* reduction and write to output vector */
253: #pragma unroll
254: for (int offset = WARP_SIZE / 2; offset >= sliceheight; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset);
255: /* transpose layout to reduce each row using warp shfl */
256: if (threadIdx.x < sliceheight) shared[threadIdx.x * BLOCKY + threadIdx.y] = t; /* shared[threadIdx.x][threadIdx.y] = t */
257: __syncthreads();
258: if (tidy < sliceheight) t = shared[tidy * BLOCKY + tidx]; /* shared[tidy][tidx] */
259: #pragma unroll
260: for (int offset = BLOCKY / 2; offset > 0; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset, BLOCKY);
261: if (tidx == 0 && tidy < sliceheight) shared[tidy] = t; /* shared[0][tidy] = t */
262: __syncthreads();
263: if (row < nrows && threadIdx.y == 0 && threadIdx.x < sliceheight) atomAdd(y[row], shared[threadIdx.x]); /* shared[0][threadIdx.x] */
264: t = 0.0;
265: }
266: }
267: }
268: }
269: }
271: /* load-balancing version. Chunksize is equal to the number of threads per block */
272: template <int BLOCKY>
273: __global__ void matmultadd_seqsell_tiled_kernel8(PetscInt nrows, PetscInt sliceheight, PetscInt chunksperblock, PetscInt totalchunks, const PetscInt *chunk_slice_map, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
274: {
275: __shared__ MatScalar shared[BLOCKY * WARP_SIZE];
276: PetscInt gid, row, start_slice, cid;
277: PetscScalar t = 0.0;
278: AtomicAdd<MatScalar> atomAdd;
279: /* copy y to z */
280: for (int iter = 0; iter < 1 + (nrows - 1) / (gridDim.x * WARP_SIZE * BLOCKY); iter++) {
281: gid = gridDim.x * WARP_SIZE * BLOCKY * iter + blockIdx.x * BLOCKY * WARP_SIZE + threadIdx.y * WARP_SIZE + threadIdx.x;
282: if (gid < nrows) z[gid] = y[gid];
283: }
284: for (int iter = 0; iter < chunksperblock; iter++) {
285: cid = blockIdx.x * chunksperblock + iter; /* chunk id */
286: if (cid < totalchunks) {
287: start_slice = chunk_slice_map[cid]; /* starting slice at each iteration */
288: gid = cid * BLOCKY * WARP_SIZE + threadIdx.y * WARP_SIZE + threadIdx.x;
289: if ((cid + 1) * BLOCKY * WARP_SIZE > sliidx[start_slice + 1]) { /* this iteration covers more than one slice */
290: __shared__ PetscInt flag[BLOCKY * 2];
291: bool write;
292: PetscInt slice_id = start_slice, totalslices = PetscCeilIntMacro(nrows, sliceheight), totalentries = sliidx[totalslices];
293: /* find out the slice that this element belongs to */
294: while (gid < totalentries && gid >= sliidx[slice_id + 1]) slice_id++;
295: if (threadIdx.x % (WARP_SIZE / 2) == 0) flag[threadIdx.y * 2 + threadIdx.x / (WARP_SIZE / 2)] = slice_id;
296: row = slice_id * sliceheight + threadIdx.x % sliceheight;
297: if (row < nrows && gid < totalentries) t = aval[gid] * x[acolidx[gid]];
298: __syncthreads();
299: write = segment_scan<BLOCKY>(flag, shared, &t);
300: if (row < nrows && gid < totalentries && write) atomAdd(z[row], t);
301: t = 0.0;
302: } else { /* this iteration covers only one slice */
303: row = start_slice * sliceheight + threadIdx.x % sliceheight;
304: if (row < nrows) t += aval[gid] * x[acolidx[gid]];
305: if (iter == chunksperblock - 1 || (cid + 2) * BLOCKY * WARP_SIZE > sliidx[start_slice + 1]) { /* last iteration or next iteration covers more than one slice */
306: int tid = threadIdx.x + threadIdx.y * WARP_SIZE, tidx = tid % BLOCKY, tidy = tid / BLOCKY;
307: /* reduction and write to output vector */
308: #pragma unroll
309: for (int offset = WARP_SIZE / 2; offset >= sliceheight; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset);
310: /* transpose layout to reduce each row using warp shfl */
311: if (threadIdx.x < sliceheight) shared[threadIdx.x * BLOCKY + threadIdx.y] = t; /* shared[threadIdx.x][threadIdx.y] = t */
312: __syncthreads();
313: if (tidy < sliceheight) t = shared[tidy * BLOCKY + tidx]; /* shared[tidy][tidx] */
314: #pragma unroll
315: for (int offset = BLOCKY / 2; offset > 0; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset, BLOCKY);
316: if (tidx == 0 && tidy < sliceheight) shared[tidy] = t; /* shared[0][tidy] = t */
317: __syncthreads();
318: if (row < nrows && threadIdx.y == 0 && threadIdx.x < sliceheight) atomAdd(z[row], shared[threadIdx.x]); /* shared[0][threadIdx.x] */
319: t = 0.0;
320: }
321: }
322: }
323: }
324: }
326: /* use 1 warp per slice, suitable for small slice width */
327: static __global__ void matmult_seqsell_tiled_kernel7(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
328: {
329: PetscInt i, row, slice_id;
330: slice_id = blockIdx.x * blockDim.y + threadIdx.y;
331: row = slice_id * sliceheight + threadIdx.x % sliceheight;
332: double t = 0.0;
333: if (row < nrows) {
334: for (i = sliidx[slice_id] + threadIdx.x; i < sliidx[slice_id + 1]; i += WARP_SIZE) t += aval[i] * x[acolidx[i]];
335: }
336: #pragma unroll
337: for (int offset = WARP_SIZE / 2; offset >= sliceheight; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset);
338: if (row < nrows && threadIdx.x < sliceheight) y[row] = t;
339: }
341: /* use 1 warp per slice, suitable for small slice width */
342: static __global__ void matmultadd_seqsell_tiled_kernel7(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
343: {
344: PetscInt i, row, slice_id;
345: slice_id = blockIdx.x * blockDim.y + threadIdx.y;
346: row = slice_id * sliceheight + threadIdx.x % sliceheight;
347: double t = 0.0;
348: if (row < nrows) {
349: for (i = sliidx[slice_id] + threadIdx.x; i < sliidx[slice_id + 1]; i += WARP_SIZE) t += aval[i] * x[acolidx[i]];
350: }
351: #pragma unroll
352: for (int offset = WARP_SIZE / 2; offset >= sliceheight; offset /= 2) t += MatSeqSELLHIPShuffleDown(t, offset);
353: if (row < nrows && threadIdx.x < sliceheight) z[row] = y[row] + t;
354: }
355: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END()
356: #endif
358: /*********** Kernel 2-6 require a slice height smaller than 512, 256, 128, 64, 32, espectively. They are kept only for performance comparison **********/
360: static __global__ void matmult_seqsell_tiled_kernel6(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
361: {
362: __shared__ MatScalar shared[32 * 16];
363: PetscInt i, row, slice_id, row_in_slice;
364: /* multiple threads per row. */
365: row = blockIdx.x * blockDim.x + threadIdx.x;
366: if (row < nrows) {
367: slice_id = row / sliceheight;
368: row_in_slice = row % sliceheight;
370: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
371: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
372: __syncthreads();
373: if (threadIdx.y < 16) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 16) * blockDim.x + threadIdx.x];
374: __syncthreads();
375: if (threadIdx.y < 8) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 8) * blockDim.x + threadIdx.x];
376: __syncthreads();
377: if (threadIdx.y < 4) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 4) * blockDim.x + threadIdx.x];
378: __syncthreads();
379: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
380: __syncthreads();
381: if (threadIdx.y < 1) {
382: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
383: y[row] = shared[threadIdx.x];
384: }
385: }
386: }
388: static __global__ void matmult_seqsell_tiled_kernel5(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
389: {
390: __shared__ MatScalar shared[32 * 16];
391: PetscInt i, row, slice_id, row_in_slice;
392: /* multiple threads per row. */
393: row = blockIdx.x * blockDim.x + threadIdx.x;
394: if (row < nrows) {
395: slice_id = row / sliceheight;
396: row_in_slice = row % sliceheight;
398: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
399: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
400: __syncthreads();
401: if (threadIdx.y < 8) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 8) * blockDim.x + threadIdx.x];
402: __syncthreads();
403: if (threadIdx.y < 4) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 4) * blockDim.x + threadIdx.x];
404: __syncthreads();
405: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
406: __syncthreads();
407: if (threadIdx.y < 1) {
408: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
409: y[row] = shared[threadIdx.x];
410: }
411: }
412: }
414: static __global__ void matmult_seqsell_tiled_kernel4(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
415: {
416: __shared__ MatScalar shared[32 * 16];
417: PetscInt i, row, slice_id, row_in_slice;
418: /* multiple threads per row. */
419: row = blockIdx.x * blockDim.x + threadIdx.x;
420: if (row < nrows) {
421: slice_id = row / sliceheight;
422: row_in_slice = row % sliceheight;
424: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
425: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
426: __syncthreads();
427: if (threadIdx.y < 4) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 4) * blockDim.x + threadIdx.x];
428: __syncthreads();
429: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
430: __syncthreads();
431: if (threadIdx.y < 1) {
432: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
433: y[row] = shared[threadIdx.x];
434: }
435: }
436: }
438: static __global__ void matmult_seqsell_tiled_kernel3(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
439: {
440: __shared__ MatScalar shared[32 * 16];
441: PetscInt i, row, slice_id, row_in_slice;
442: /* multiple threads per row. */
443: row = blockIdx.x * blockDim.x + threadIdx.x;
444: if (row < nrows) {
445: slice_id = row / sliceheight;
446: row_in_slice = row % sliceheight;
448: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
449: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
450: __syncthreads();
451: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
452: __syncthreads();
453: if (threadIdx.y < 1) {
454: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
455: y[row] = shared[threadIdx.x];
456: }
457: }
458: }
460: static __global__ void matmult_seqsell_tiled_kernel2(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, PetscScalar *y)
461: {
462: __shared__ MatScalar shared[32 * 16];
463: PetscInt i, row, slice_id, row_in_slice;
464: /* multiple threads per row. */
465: row = blockIdx.x * blockDim.x + threadIdx.x;
466: if (row < nrows) {
467: slice_id = row / sliceheight;
468: row_in_slice = row % sliceheight;
470: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
471: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
472: __syncthreads();
473: if (threadIdx.y < 1) {
474: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
475: y[row] = shared[threadIdx.x];
476: }
477: }
478: }
480: static __global__ void matmultadd_seqsell_tiled_kernel6(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
481: {
482: __shared__ MatScalar shared[32 * 16];
483: PetscInt i, row, slice_id, row_in_slice;
484: /* multiple threads per row. */
485: row = blockIdx.x * blockDim.x + threadIdx.x;
486: if (row < nrows) {
487: slice_id = row / sliceheight;
488: row_in_slice = row % sliceheight;
490: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
491: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
492: __syncthreads();
493: if (threadIdx.y < 16) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 16) * blockDim.x + threadIdx.x];
494: __syncthreads();
495: if (threadIdx.y < 8) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 8) * blockDim.x + threadIdx.x];
496: __syncthreads();
497: if (threadIdx.y < 4) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 4) * blockDim.x + threadIdx.x];
498: __syncthreads();
499: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
500: __syncthreads();
501: if (threadIdx.y < 1) {
502: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
503: z[row] = y[row] + shared[threadIdx.x];
504: }
505: }
506: }
508: static __global__ void matmultadd_seqsell_tiled_kernel5(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
509: {
510: __shared__ MatScalar shared[32 * 16];
511: PetscInt i, row, slice_id, row_in_slice;
512: /* multiple threads per row. */
513: row = blockIdx.x * blockDim.x + threadIdx.x;
514: if (row < nrows) {
515: slice_id = row / sliceheight;
516: row_in_slice = row % sliceheight;
518: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
519: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
520: __syncthreads();
521: if (threadIdx.y < 8) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 8) * blockDim.x + threadIdx.x];
522: __syncthreads();
523: if (threadIdx.y < 4) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 4) * blockDim.x + threadIdx.x];
524: __syncthreads();
525: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
526: __syncthreads();
527: if (threadIdx.y < 1) {
528: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
529: z[row] = y[row] + shared[threadIdx.x];
530: }
531: }
532: }
534: static __global__ void matmultadd_seqsell_tiled_kernel4(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
535: {
536: __shared__ MatScalar shared[32 * 16];
537: PetscInt i, row, slice_id, row_in_slice;
538: /* multiple threads per row. */
539: row = blockIdx.x * blockDim.x + threadIdx.x;
540: if (row < nrows) {
541: slice_id = row / sliceheight;
542: row_in_slice = row % sliceheight;
544: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
545: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
546: __syncthreads();
547: if (threadIdx.y < 4) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 4) * blockDim.x + threadIdx.x];
548: __syncthreads();
549: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
550: __syncthreads();
551: if (threadIdx.y < 1) {
552: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
553: z[row] = y[row] + shared[threadIdx.x];
554: }
555: }
556: }
558: static __global__ void matmultadd_seqsell_tiled_kernel3(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
559: {
560: __shared__ MatScalar shared[32 * 16];
561: PetscInt i, row, slice_id, row_in_slice;
562: /* multiple threads per row. */
563: row = blockIdx.x * blockDim.x + threadIdx.x;
564: if (row < nrows) {
565: slice_id = row / sliceheight;
566: row_in_slice = row % sliceheight;
568: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
569: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
570: __syncthreads();
571: if (threadIdx.y < 2) shared[threadIdx.y * blockDim.x + threadIdx.x] += shared[(threadIdx.y + 2) * blockDim.x + threadIdx.x];
572: __syncthreads();
573: if (threadIdx.y < 1) {
574: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
575: z[row] = y[row] + shared[threadIdx.x];
576: }
577: }
578: }
580: static __global__ void matmultadd_seqsell_tiled_kernel2(PetscInt nrows, PetscInt sliceheight, const PetscInt *acolidx, const MatScalar *aval, const PetscInt *sliidx, const PetscScalar *x, const PetscScalar *y, PetscScalar *z)
581: {
582: __shared__ MatScalar shared[32 * 16];
583: PetscInt i, row, slice_id, row_in_slice;
584: /* multiple threads per row. */
585: row = blockIdx.x * blockDim.x + threadIdx.x;
586: if (row < nrows) {
587: slice_id = row / sliceheight;
588: row_in_slice = row % sliceheight;
590: shared[threadIdx.y * blockDim.x + threadIdx.x] = 0.0;
591: for (i = sliidx[slice_id] + row_in_slice + sliceheight * threadIdx.y; i < sliidx[slice_id + 1]; i += sliceheight * blockDim.y) shared[threadIdx.y * blockDim.x + threadIdx.x] += aval[i] * x[acolidx[i]];
592: __syncthreads();
593: if (threadIdx.y < 1) {
594: shared[threadIdx.x] += shared[blockDim.x + threadIdx.x];
595: z[row] = y[row] + shared[threadIdx.x];
596: }
597: }
598: }
600: static PetscErrorCode MatMult_SeqSELLHIP(Mat A, Vec xx, Vec yy)
601: {
602: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
603: Mat_SeqSELLHIP *hipstruct = (Mat_SeqSELLHIP *)A->spptr;
604: PetscScalar *y;
605: const PetscScalar *x;
606: PetscInt nrows = A->rmap->n, sliceheight = a->sliceheight;
607: MatScalar *aval;
608: PetscInt *acolidx;
609: PetscInt *sliidx;
610: PetscInt nblocks, blocksize = 512; /* blocksize is fixed to be 512 */
611: dim3 block2(256, 2), block4(128, 4), block8(64, 8), block16(32, 16), block32(16, 32);
612: #if !PetscDefined(USE_COMPLEX)
613: PetscInt chunksperblock, nchunks, *chunk_slice_map;
614: PetscReal maxoveravg;
615: #endif
616: PetscDeviceContext dctx;
617: hipStream_t stream;
619: PetscFunctionBegin;
620: PetscCall(MatSeqSELLHIPGetContext(&dctx, &stream));
621: PetscCheck(WARP_SIZE % sliceheight == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "The kernel requires a slice height be a divisor of WARP_SIZE, but the input matrix has a slice height of %" PetscInt_FMT, sliceheight);
622: PetscCheck(!(hipstruct->kernelchoice >= 2 && hipstruct->kernelchoice <= 6 && sliceheight > 32), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Kernel choices 2-6 require a slice height of at most 32, but the current slice height is %" PetscInt_FMT, sliceheight);
623: PetscCall(MatSeqSELLHIPCopyToGPU(A));
624: /* hipstruct may not be available until MatSeqSELLHIPCopyToGPU() is called */
625: aval = hipstruct->val;
626: acolidx = hipstruct->colidx;
627: sliidx = hipstruct->sliidx;
629: PetscCall(VecHIPGetArrayRead(xx, &x));
630: PetscCall(VecHIPGetArrayWrite(yy, &y));
631: PetscCall(PetscLogGpuTimeBegin());
633: switch (hipstruct->kernelchoice) {
634: #if !PetscDefined(USE_COMPLEX)
635: case 9: /* 1 slice per block */
636: nblocks = 1 + (nrows - 1) / sliceheight;
637: if (hipstruct->blocky == 2) {
638: matmult_seqsell_tiled_kernel9<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
639: } else if (hipstruct->blocky == 4) {
640: matmult_seqsell_tiled_kernel9<4><<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
641: } else if (hipstruct->blocky == 8) {
642: matmult_seqsell_tiled_kernel9<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
643: } else if (hipstruct->blocky == 16) {
644: matmult_seqsell_tiled_kernel9<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
645: } else {
646: matmult_seqsell_tiled_kernel9<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
647: }
648: break;
649: case 7: /* each block handles blocky slices */
650: nblocks = 1 + (nrows - 1) / (hipstruct->blocky * sliceheight);
651: if (hipstruct->blocky == 2) {
652: matmult_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
653: } else if (hipstruct->blocky == 4) {
654: matmult_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
655: } else if (hipstruct->blocky == 8) {
656: matmult_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
657: } else if (hipstruct->blocky == 16) {
658: matmult_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
659: } else {
660: nblocks = 1 + (nrows - 1) / (2 * sliceheight);
661: matmult_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
662: }
663: break;
664: #endif
665: case 6:
666: nblocks = 1 + (nrows - 1) / (blocksize / 32); /* 1 slice per block if sliceheight=32 */
667: matmult_seqsell_tiled_kernel6<<<nblocks, block32, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
668: break;
669: case 5:
670: nblocks = 1 + (nrows - 1) / (blocksize / 16); /* 2 slices per block if sliceheight=32*/
671: matmult_seqsell_tiled_kernel5<<<nblocks, block16, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
672: break;
673: case 4:
674: nblocks = 1 + (nrows - 1) / (blocksize / 8); /* 4 slices per block if sliceheight=32 */
675: matmult_seqsell_tiled_kernel4<<<nblocks, block8, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
676: break;
677: case 3:
678: nblocks = 1 + (nrows - 1) / (blocksize / 4); /* 8 slices per block if sliceheight=32 */
679: matmult_seqsell_tiled_kernel3<<<nblocks, block4, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
680: break;
681: case 2: /* 16 slices per block if sliceheight=32 */
682: nblocks = 1 + (nrows - 1) / (blocksize / 2);
683: matmult_seqsell_tiled_kernel2<<<nblocks, block2, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
684: break;
685: case 1: /* 32 slices per block if sliceheight=32 */
686: nblocks = 1 + (nrows - 1) / blocksize;
687: matmult_seqsell_basic_kernel<<<nblocks, blocksize, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
688: break;
689: #if !PetscDefined(USE_COMPLEX)
690: case 0:
691: maxoveravg = a->maxslicewidth / a->avgslicewidth;
692: if (maxoveravg > 12.0 && maxoveravg / nrows > 0.001) { /* important threshold */
693: /* each block handles approximately one slice */
694: PetscInt blocky = a->chunksize / 32;
695: nchunks = hipstruct->totalchunks;
696: chunksperblock = hipstruct->chunksperblock ? hipstruct->chunksperblock : 1 + (hipstruct->totalentries / hipstruct->totalslices - 1) / a->chunksize;
697: nblocks = 1 + (nchunks - 1) / chunksperblock;
698: chunk_slice_map = hipstruct->chunk_slice_map;
699: if (blocky == 2) {
700: matmult_seqsell_tiled_kernel8<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y);
701: } else if (blocky == 4) {
702: matmult_seqsell_tiled_kernel8<4><<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y);
703: } else if (blocky == 8) {
704: matmult_seqsell_tiled_kernel8<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y);
705: } else if (blocky == 16) {
706: matmult_seqsell_tiled_kernel8<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y);
707: } else {
708: matmult_seqsell_tiled_kernel8<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y);
709: }
710: } else {
711: PetscInt avgslicesize = sliceheight * a->avgslicewidth;
712: if (avgslicesize <= 432) {
713: if (sliceheight * a->maxslicewidth < 2048 && nrows > 100000) {
714: nblocks = 1 + (nrows - 1) / (2 * sliceheight); /* two slices per block */
715: matmult_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
716: } else {
717: nblocks = 1 + (nrows - 1) / sliceheight;
718: matmult_seqsell_tiled_kernel9<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
719: }
720: } else if (avgslicesize <= 2400) {
721: nblocks = 1 + (nrows - 1) / sliceheight;
722: matmult_seqsell_tiled_kernel9<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
723: } else {
724: nblocks = 1 + (nrows - 1) / sliceheight;
725: matmult_seqsell_tiled_kernel9<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y);
726: }
727: }
728: break;
729: #endif
730: default:
731: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "unsupported kernel choice %" PetscInt_FMT " for MatMult_SeqSELLHIP.", hipstruct->kernelchoice);
732: }
733: PetscCallHIP(hipGetLastError());
734: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
735: PetscCall(PetscLogGpuTimeEnd());
736: PetscCall(VecHIPRestoreArrayRead(xx, &x));
737: PetscCall(VecHIPRestoreArrayWrite(yy, &y));
738: PetscCall(PetscLogGpuFlops(2.0 * a->nz - a->nonzerorowcnt));
739: PetscFunctionReturn(PETSC_SUCCESS);
740: }
742: static PetscErrorCode MatMultAdd_SeqSELLHIP(Mat A, Vec xx, Vec yy, Vec zz)
743: {
744: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
745: Mat_SeqSELLHIP *hipstruct = (Mat_SeqSELLHIP *)A->spptr;
746: PetscScalar *z;
747: const PetscScalar *y, *x;
748: PetscInt nrows = A->rmap->n, sliceheight = a->sliceheight;
749: MatScalar *aval;
750: PetscInt *acolidx;
751: PetscInt *sliidx;
752: #if !PetscDefined(USE_COMPLEX)
753: PetscReal maxoveravg;
754: PetscInt chunksperblock, nchunks, *chunk_slice_map;
755: PetscInt blocky = hipstruct->blocky;
756: #endif
757: PetscDeviceContext dctx;
758: hipStream_t stream;
760: PetscFunctionBegin;
761: PetscCall(MatSeqSELLHIPGetContext(&dctx, &stream));
762: PetscCheck(WARP_SIZE % sliceheight == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "The kernel requires a slice height be a divisor of WARP_SIZE, but the input matrix has a slice height of %" PetscInt_FMT, sliceheight);
763: PetscCheck(!(hipstruct->kernelchoice >= 2 && hipstruct->kernelchoice <= 6 && sliceheight > 32), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Kernel choices 2-6 require a slice height of at most 32, but the current slice height is %" PetscInt_FMT, sliceheight);
764: PetscCall(MatSeqSELLHIPCopyToGPU(A));
765: aval = hipstruct->val;
766: acolidx = hipstruct->colidx;
767: sliidx = hipstruct->sliidx;
768: if (a->nz) {
769: PetscInt nblocks, blocksize = 512;
770: dim3 block2(256, 2), block4(128, 4), block8(64, 8), block16(32, 16), block32(16, 32);
771: PetscCall(VecHIPGetArrayRead(xx, &x));
772: PetscCall(VecHIPGetArrayRead(yy, &y));
773: PetscCall(VecHIPGetArrayWrite(zz, &z));
774: PetscCall(PetscLogGpuTimeBegin());
776: switch (hipstruct->kernelchoice) {
777: #if !PetscDefined(USE_COMPLEX)
778: case 9:
779: nblocks = 1 + (nrows - 1) / sliceheight;
780: if (blocky == 2) {
781: matmultadd_seqsell_tiled_kernel9<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
782: } else if (blocky == 4) {
783: matmultadd_seqsell_tiled_kernel9<4><<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
784: } else if (blocky == 8) {
785: matmultadd_seqsell_tiled_kernel9<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
786: } else if (blocky == 16) {
787: matmultadd_seqsell_tiled_kernel9<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
788: } else {
789: matmultadd_seqsell_tiled_kernel9<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
790: }
791: break;
792: case 8:
793: /* each block handles approximately one slice */
794: nchunks = hipstruct->totalchunks;
795: blocky = a->chunksize / 32;
796: chunksperblock = hipstruct->chunksperblock ? hipstruct->chunksperblock : 1 + (hipstruct->totalentries / hipstruct->totalslices - 1) / a->chunksize;
797: nblocks = 1 + (nchunks - 1) / chunksperblock;
798: chunk_slice_map = hipstruct->chunk_slice_map;
799: if (blocky == 2) {
800: matmultadd_seqsell_tiled_kernel8<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
801: } else if (blocky == 4) {
802: matmultadd_seqsell_tiled_kernel8<4><<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
803: } else if (blocky == 8) {
804: matmultadd_seqsell_tiled_kernel8<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
805: } else if (blocky == 16) {
806: matmultadd_seqsell_tiled_kernel8<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
807: } else {
808: matmultadd_seqsell_tiled_kernel8<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
809: }
810: break;
811: case 7:
812: nblocks = 1 + (nrows - 1) / (blocky * sliceheight);
813: if (blocky == 2) {
814: matmultadd_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
815: } else if (blocky == 4) {
816: matmultadd_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
817: } else if (blocky == 8) {
818: matmultadd_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
819: } else if (blocky == 16) {
820: matmultadd_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
821: } else {
822: nblocks = 1 + (nrows - 1) / (2 * sliceheight);
823: matmultadd_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
824: }
825: break;
826: #endif
827: case 6:
828: nblocks = 1 + (nrows - 1) / (blocksize / 32);
829: matmultadd_seqsell_tiled_kernel6<<<nblocks, block32, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
830: break;
831: case 5:
832: nblocks = 1 + (nrows - 1) / (blocksize / 16);
833: matmultadd_seqsell_tiled_kernel5<<<nblocks, block16, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
834: break;
835: case 4:
836: nblocks = 1 + (nrows - 1) / (blocksize / 8);
837: matmultadd_seqsell_tiled_kernel4<<<nblocks, block8, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
838: break;
839: case 3:
840: nblocks = 1 + (nrows - 1) / (blocksize / 4);
841: matmultadd_seqsell_tiled_kernel3<<<nblocks, block4, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
842: break;
843: case 2:
844: nblocks = 1 + (nrows - 1) / (blocksize / 2);
845: matmultadd_seqsell_tiled_kernel2<<<nblocks, block2, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
846: break;
847: case 1:
848: nblocks = 1 + (nrows - 1) / blocksize;
849: matmultadd_seqsell_basic_kernel<<<nblocks, blocksize, 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
850: break;
851: #if !PetscDefined(USE_COMPLEX)
852: case 0:
853: maxoveravg = a->maxslicewidth / a->avgslicewidth;
854: if (maxoveravg > 12.0 && maxoveravg / nrows > 0.001) { /* important threshold */
855: /* each block handles approximately one slice */
856: nchunks = hipstruct->totalchunks;
857: blocky = a->chunksize / 32;
858: chunksperblock = hipstruct->chunksperblock ? hipstruct->chunksperblock : 1 + (hipstruct->totalentries / hipstruct->totalslices - 1) / a->chunksize;
859: nblocks = 1 + (nchunks - 1) / chunksperblock;
860: chunk_slice_map = hipstruct->chunk_slice_map;
861: if (blocky == 2) {
862: matmultadd_seqsell_tiled_kernel8<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
863: } else if (blocky == 4) {
864: matmultadd_seqsell_tiled_kernel8<4><<<nblocks, dim3(WARP_SIZE, 4), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
865: } else if (blocky == 8) {
866: matmultadd_seqsell_tiled_kernel8<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
867: } else if (blocky == 16) {
868: matmultadd_seqsell_tiled_kernel8<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
869: } else {
870: matmultadd_seqsell_tiled_kernel8<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, chunksperblock, nchunks, chunk_slice_map, acolidx, aval, sliidx, x, y, z);
871: }
872: } else {
873: PetscInt avgslicesize = sliceheight * a->avgslicewidth;
874: if (avgslicesize <= 432) {
875: if (sliceheight * a->maxslicewidth < 2048 && nrows > 100000) {
876: nblocks = 1 + (nrows - 1) / (2 * sliceheight); /* two slices per block */
877: matmultadd_seqsell_tiled_kernel7<<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
878: } else {
879: nblocks = 1 + (nrows - 1) / sliceheight;
880: matmultadd_seqsell_tiled_kernel9<2><<<nblocks, dim3(WARP_SIZE, 2), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
881: }
882: } else if (avgslicesize <= 2400) {
883: nblocks = 1 + (nrows - 1) / sliceheight;
884: matmultadd_seqsell_tiled_kernel9<8><<<nblocks, dim3(WARP_SIZE, 8), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
885: } else {
886: nblocks = 1 + (nrows - 1) / sliceheight;
887: matmultadd_seqsell_tiled_kernel9<16><<<nblocks, dim3(WARP_SIZE, 16), 0, stream>>>(nrows, sliceheight, acolidx, aval, sliidx, x, y, z);
888: }
889: }
890: break;
891: #endif
892: default:
893: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "unsupported kernel choice %" PetscInt_FMT " for MatMult_SeqSELLHIP.", hipstruct->kernelchoice);
894: }
895: PetscCallHIP(hipGetLastError());
896: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
897: PetscCall(PetscLogGpuTimeEnd());
898: PetscCall(VecHIPRestoreArrayRead(xx, &x));
899: PetscCall(VecHIPRestoreArrayRead(yy, &y));
900: PetscCall(VecHIPRestoreArrayWrite(zz, &z));
901: PetscCall(PetscLogGpuFlops(2.0 * a->nz));
902: } else {
903: PetscCall(VecCopy(yy, zz));
904: }
905: PetscFunctionReturn(PETSC_SUCCESS);
906: }
908: static PetscErrorCode MatSetFromOptions_SeqSELLHIP(Mat A, PetscOptionItems PetscOptionsObject)
909: {
910: Mat_SeqSELLHIP *hipstruct = (Mat_SeqSELLHIP *)A->spptr;
911: PetscInt kernel, blocky;
912: PetscBool flg;
914: PetscFunctionBegin;
915: PetscOptionsHeadBegin(PetscOptionsObject, "SeqSELLHIP options");
916: PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_sell_spmv_hip_blocky", &blocky, &flg));
917: if (flg) {
918: PetscCheck(blocky == 2 || blocky == 4 || blocky == 8 || blocky == 16 || blocky == 32, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported blocky: %" PetscInt_FMT " it should be in {2,4,8,16,32}", blocky);
919: hipstruct->blocky = blocky;
920: }
921: PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_sell_spmv_hip_kernel", &kernel, &flg));
922: if (flg) {
923: PetscCheck(kernel >= 0 && kernel <= 9, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Wrong kernel choice: %" PetscInt_FMT " it should be in [0,9]", kernel);
924: hipstruct->kernelchoice = kernel;
925: if (kernel == 8) PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_sell_spmv_hip_chunksperblock", &hipstruct->chunksperblock, &flg));
926: }
927: PetscOptionsHeadEnd();
928: PetscFunctionReturn(PETSC_SUCCESS);
929: }
931: PETSC_INTERN PetscErrorCode MatAssemblyEnd_SpMV_Preprocessing_Private(Mat A)
932: {
933: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
935: PetscFunctionBegin;
936: PetscCall(MatSeqSELLGetAvgSliceWidth(A, &a->avgslicewidth));
937: PetscCall(MatSeqSELLGetMaxSliceWidth(A, &a->maxslicewidth));
938: PetscCall(MatSeqSELLGetFillRatio(A, &a->fillratio));
939: PetscFunctionReturn(PETSC_SUCCESS);
940: }
942: static PetscErrorCode MatAssemblyEnd_SeqSELLHIP(Mat A, MatAssemblyType mode)
943: {
944: PetscFunctionBegin;
945: PetscCall(MatAssemblyEnd_SeqSELL(A, mode));
946: PetscCall(MatAssemblyEnd_SpMV_Preprocessing_Private(A));
947: if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(PETSC_SUCCESS);
948: if (A->factortype == MAT_FACTOR_NONE) PetscCall(MatSeqSELLHIPCopyToGPU(A));
949: A->ops->mult = MatMult_SeqSELLHIP;
950: A->ops->multadd = MatMultAdd_SeqSELLHIP;
951: PetscFunctionReturn(PETSC_SUCCESS);
952: }
954: static PetscErrorCode MatZeroEntries_SeqSELLHIP(Mat A)
955: {
956: PetscBool both = PETSC_FALSE;
957: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
958: PetscDeviceContext dctx;
959: hipStream_t stream;
961: PetscFunctionBegin;
962: PetscCall(MatSeqSELLHIPGetContext(&dctx, &stream));
963: if (A->factortype == MAT_FACTOR_NONE) {
964: Mat_SeqSELLHIP *hipstruct = (Mat_SeqSELLHIP *)A->spptr;
965: if (hipstruct->val) {
966: both = PETSC_TRUE;
967: PetscCallHIP(hipMemsetAsync(hipstruct->val, 0, a->sliidx[a->totalslices] * sizeof(*hipstruct->val), stream));
968: }
969: }
970: PetscCall(PetscArrayzero(a->val, a->sliidx[a->totalslices]));
971: if (both) A->offloadmask = PETSC_OFFLOAD_BOTH;
972: else A->offloadmask = PETSC_OFFLOAD_CPU;
973: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
974: PetscFunctionReturn(PETSC_SUCCESS);
975: }
977: static PetscErrorCode MatDestroy_SeqSELLHIP(Mat A)
978: {
979: PetscFunctionBegin;
980: if (A->factortype == MAT_FACTOR_NONE) PetscCall(MatSeqSELLHIP_Destroy((Mat_SeqSELLHIP **)&A->spptr));
981: PetscCall(MatDestroy_SeqSELL(A));
982: PetscFunctionReturn(PETSC_SUCCESS);
983: }
985: PETSC_INTERN PetscErrorCode MatConvert_SeqSELL_SeqSELLHIP(Mat);
986: static PetscErrorCode MatDuplicate_SeqSELLHIP(Mat A, MatDuplicateOption cpvalues, Mat *B)
987: {
988: PetscFunctionBegin;
989: PetscCall(MatDuplicate_SeqSELL(A, cpvalues, B));
990: PetscCall(MatConvert_SeqSELL_SeqSELLHIP(*B));
991: PetscFunctionReturn(PETSC_SUCCESS);
992: }
994: PETSC_INTERN PetscErrorCode MatConvert_SeqSELL_SeqSELLHIP(Mat B)
995: {
996: Mat_SeqSELLHIP *hipstruct;
998: PetscFunctionBegin;
999: PetscCall(PetscFree(B->defaultvectype));
1000: PetscCall(PetscStrallocpy(VECHIP, &B->defaultvectype));
1002: if (!B->spptr) {
1003: if (B->factortype == MAT_FACTOR_NONE) {
1004: PetscCall(PetscNew(&hipstruct));
1005: B->spptr = hipstruct;
1006: }
1007: }
1009: B->ops->assemblyend = MatAssemblyEnd_SeqSELLHIP;
1010: B->ops->destroy = MatDestroy_SeqSELLHIP;
1011: B->ops->setfromoptions = MatSetFromOptions_SeqSELLHIP;
1012: B->ops->mult = MatMult_SeqSELLHIP;
1013: B->ops->multadd = MatMultAdd_SeqSELLHIP;
1014: B->ops->duplicate = MatDuplicate_SeqSELLHIP;
1015: B->ops->zeroentries = MatZeroEntries_SeqSELLHIP;
1017: /* No need to assemble SeqSELL, but need to do the preprocessing for SpMV */
1018: PetscCall(MatAssemblyEnd_SpMV_Preprocessing_Private(B));
1020: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQSELLHIP));
1021: B->offloadmask = PETSC_OFFLOAD_UNALLOCATED;
1022: PetscFunctionReturn(PETSC_SUCCESS);
1023: }
1025: /*MC
1026: MATSEQSELLHIP - MATSELLHIP = "(seq)sellhip" - A matrix type to be used for sparse matrices on AMD GPUs.
1028: Options Database Keys:
1029: + -mat_type seqsellhip - sets the matrix type to "seqsellhip" during a call to `MatSetFromOptions()`
1030: . -mat_sell_spmv_hip_kernel - selects a spmv kernel for MatSELLHIP
1031: - -mat_sell_spmv_hip_blocky - sets the y dimension of the block size of the spmv kernels. These kernels use a 2D block with the x dimension equal to the wrap size (normally 64 for AMD GPUs)
1033: Level: beginner
1035: .seealso: [](ch_matrices), `Mat`, `MATSELLHIP`
1036: M*/
1038: PETSC_EXTERN PetscErrorCode MatCreate_SeqSELLHIP(Mat B)
1039: {
1040: PetscFunctionBegin;
1041: PetscCall(MatCreate_SeqSELL(B));
1042: PetscCall(MatConvert_SeqSELL_SeqSELLHIP(B));
1043: PetscCall(MatSetFromOptions(B));
1044: PetscFunctionReturn(PETSC_SUCCESS);
1045: }