matvecmult_kernel.cl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright 1993-2009 NVIDIA Corporation. All rights reserved.
  3. *
  4. * NVIDIA Corporation and its licensors retain all intellectual property and
  5. * proprietary rights in and to this software and related documentation.
  6. * Any use, reproduction, disclosure, or distribution of this software
  7. * and related documentation without an express license agreement from
  8. * NVIDIA Corporation is strictly prohibited.
  9. *
  10. * Please refer to the applicable NVIDIA end user license agreement (EULA)
  11. * associated with this source code for terms and conditions that govern
  12. * your use of this NVIDIA software.
  13. *
  14. */
  15. /* Matrix-vector multiplication: W = M * V.
  16. * Device code.
  17. *
  18. * This sample implements matrix-vector multiplication.
  19. * It has been written for clarity of exposition to illustrate various OpenCL
  20. * programming principles and optimizatoins, not with the goal of providing
  21. * the most performant generic kernel for matrix-vector multiplication.
  22. *
  23. * CUBLAS provides high-performance matrix-vector multiplication on GPU.
  24. */
  25. __kernel void matVecMult(
  26. __global float* M,
  27. __global float* V,
  28. int width, int height,
  29. __global float* W
  30. )
  31. {
  32. // Row index
  33. uint y = get_global_id(0);
  34. if (y < height)
  35. {
  36. // Row pointer
  37. const __global float* row = M + y * width;
  38. // Compute dot product
  39. float dotProduct = 0;
  40. for (int x = 0; x < width; ++x)
  41. dotProduct += row[x] * V[x];
  42. // Write result to global memory
  43. W[y] = dotProduct;
  44. }
  45. }