CUDA Networks
Functions
matrix_argmax.cu File Reference

GPU implementation of the column-wise argmax function for matrices. More...

#include "matrix.h"
#include <cuda_runtime.h>
Include dependency graph for matrix_argmax.cu:

Go to the source code of this file.

Functions

__global__ void argmax_GPU (const double *m, double *result, int rows, int cols)
 CUDA kernel for computing the argmax of each column in a matrix. More...
 

Detailed Description

GPU implementation of the column-wise argmax function for matrices.

Definition in file matrix_argmax.cu.

Function Documentation

◆ argmax_GPU()

__global__ void argmax_GPU ( const double *  m,
double *  result,
int  rows,
int  cols 
)

CUDA kernel for computing the argmax of each column in a matrix.

Parameters
mPointer to the matrix data on the GPU.
resultPointer to the result vector on the GPU.
rowsNumber of rows in the matrix.
colsNumber of columns in the matrix.

Definition at line 16 of file matrix_argmax.cu.

16  {
17  // Determine the column this thread is responsible for
18  int col = blockIdx.x * blockDim.x + threadIdx.x;
19 
20  // Proceed if the column index is within matrix bounds
21  if (col < cols) {
22  // Initialize max_val with the first element in the column and max_idx to the first row
23  double max_val = m[col];
24  int max_idx = 0;
25 
26  // Iterate through the rows to find the maximum value in the column
27  for (int row = 1; row < rows; row++) {
28  double val = m[row * cols + col]; // Access element (row, col)
29  if (val > max_val) {
30  max_val = val;
31  max_idx = row;
32  }
33  }
34 
35  // Store the index of the maximum value in the result vector for this column
36  result[col] = static_cast<double>(max_idx);
37  }
38 }