Jagadeesh Kotra
Jagadeesh Kotra

Reputation: 194

Result of Matrix Multiplication is all 0

I'm trying code this C example but in C++. C code works fine but when i try to reproduce this in C++, in result matrix the first row is all 0's. I have no idea if other rows are correct either. I have spent several hours on this but couldn't figure out why the first row of resultant matrix is all 0.

my host code:

#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 300

// we have a square matrix here!
#define AH 32;
#define AW 32;

#include <CL/opencl.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <random>



int main()
{

    int r = AH;
    int c = AW;
    int elements = r * c;
    int datasize = elements * sizeof(float);

    float *matrixA = (float *)malloc(datasize);
    float *matrixB = (float *)malloc(datasize);
    float *matrixC = (float *)malloc(datasize); //result

    float *matrixC_CPU = (float *)malloc(datasize);

    // random nyumber
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<float> gen(1.0f, 4.0f);

    // fill data
    for (size_t i = 0; i < elements; i++)
    {
        matrixA[i] = gen(mt);
        matrixB[i] = gen(mt);
    }


    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);

    for (auto platform : platforms)
    {
        std::cout << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;
        std::cout << platform.getInfo<CL_PLATFORM_VERSION>() << std::endl;
    }

    std::vector<cl::Device> devices;
    cl::Device device;

    platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices);
    
    for (auto _device : devices){
        std::cout << _device.getInfo<CL_DEVICE_NAME>() << std::endl;
    }
    device = devices[0];

    cl::Context ctx(device);

    cl::CommandQueue queue = cl::CommandQueue(ctx, device);

    cl::Buffer bufA = cl::Buffer(ctx, CL_MEM_READ_ONLY, datasize);
    queue.enqueueWriteBuffer(bufA, CL_TRUE, 0, datasize, matrixA);
    cl::Buffer bufB = cl::Buffer(ctx, CL_MEM_READ_ONLY, datasize);
    queue.enqueueWriteBuffer(bufB, CL_TRUE, 0, datasize, matrixB);
    cl::Buffer bufC = cl::Buffer(ctx, CL_MEM_WRITE_ONLY, datasize);
    queue.enqueueWriteBuffer(bufC, CL_TRUE, 0, datasize, matrixC);

    std::ifstream f("kernel.cl", std::ios::in);
    std::string kernelSource;

    std::stringstream ss;
    ss << f.rdbuf();
    kernelSource = ss.str();

    std::cout << kernelSource << std::endl;

    cl::Program program(ctx, kernelSource.c_str());
    program.build(device);

    cl::Kernel vec_add_kernel(program, "matrixMul");
    vec_add_kernel.setArg(0, bufC);
    vec_add_kernel.setArg(1, bufB);
    vec_add_kernel.setArg(2, bufA);
    vec_add_kernel.setArg(3, r);
    vec_add_kernel.setArg(4, c);

    cl::NDRange global(r, c);
    cl::NDRange local(32, 32);

    queue.enqueueNDRangeKernel(vec_add_kernel, 0, global, local);

    queue.enqueueReadBuffer(bufC, CL_TRUE, 0, datasize, matrixC);

    for (int i = 0; i < elements; i++)
    {
        if (i % r == 0)
            std::cout << std::endl;
        std::cout << matrixC[i] << " ";
    }


    return 0;

}

kernel.cl


/* kernel.cl 
 * Matrix multiplication: C = A * B.
 * Device code.
 */
 
// OpenCL Kernel
__kernel void
matrixMul(__global float* C, 
          __global float* A, 
          __global float* B, 
          int wA, int wB)
{
  
   int tx = get_global_id(0); 
   int ty = get_global_id(1);
 
   // value stores the element that is 
   // computed by the thread
   float value = 0;
   for (int k = 0; k < wA; ++k)
   {
      float elementA = A[ty * wA + k];
      float elementB = B[k * wB + tx];
      value += elementA * elementB;
   }
 
   // Write the matrix to device memory each 
   // thread writes one element
   C[ty * wA + tx] = value;
}

Upvotes: 0

Views: 111

Answers (1)

ProjectPhysX
ProjectPhysX

Reputation: 5764

Strange, your code is working.

I just changed these 2 lines:

#include <CL/cl.hpp>
program.build();

What also might be different is that I used the OpenCL 1.2 headers from here. What device do you use? Is there maybe issues with the GPU drivers?

I get this output:

NVIDIA CUDA
OpenCL 3.0 CUDA 11.5.125
Intel(R) OpenCL
OpenCL 2.1
NVIDIA TITAN Xp

202.031 222.182 192.989 187.79 208.397 205.866 200.2 198.824 200.881 241.385 213.799 212.529 226.955 199.066 199.734 185.697 213.838 201.491 227.899 213.216 192.799 195.562 214.389 222.303 208.798 216.081 195.191 235.627 221.991 217.427 195.229 221.698
205.164 222.377 186.033 185.05 205.337 215.021 207.147 197.748 195.935 246.367 220.326 214.799 226.878 198.987 197.654 183.152 203.486 204.59 216.372 212.38 185.489 197.275 213.582 214.374 204.808 208.008 199.53 229.753 217.432 223.844 194.783 214.185
216.11 234.159 196.06 188.031 220.596 216.944 214.938 202.753 204.683 251.203 224.894 226.764 241.11 204.342 201.271 188.837 225.943 206.965 223.583 221.181 192.662 198.016 226.64 225.576 206.186 220.61 210.13 230.772 230.827 230.447 207.167 231.28
175.142 189.147 160.4 167.924 181.792 178.692 176.536 167.147 161.678 201.673 179.299 186.606 192.253 175.515 170.971 156.797 185.716 171.885 194.157 185.934 149.641 161.185 184.93 177.779 178.531 183.586 175.678 198.014 189.519 185.488 164.419 195.802
176.866 187.418 175.031 163.775 184.117 174.348 177.46 176.764 168.25 215.393 195.116 196.997 194.735 177.338 170.552 164.152 189.253 185.529 195.284 185.331 161.286 167.037 193.778 193.8 186.799 183.118 173.534 210.976 188.764 195.876 163.834 198.816
188.691 205.472 180.075 169.942 198.878 198.098 187.761 187.601 184.298 227.811 208.616 205.526 217.088 187 184.64 175.252 199.476 189.734 207.472 204.218 178.786 180.363 197.707 203.54 194.497 193.539 188.174 218.911 210.593 210.32 190.581 209.913
189.636 209.882 177.409 179.904 194.642 204.935 191.228 197.939 185.117 231.387 213.296 206.203 224.324 186.102 188.89 186.577 203.909 185.496 208.488 200.29 176.156 179.355 188.955 202.313 199.014 196.949 189.545 221.938 198.72 206.263 180.475 210.967
192.518 209.955 187.977 183.555 201.267 209.983 192.125 192.94 188.369 226.687 205.113 206.435 217.325 194.359 186.223 184.218 207.568 187.505 214.825 197.389 184.202 186.841 194.228 214.6 202.488 206.95 187.835 223.203 208.87 205.146 190.19 222.04
184.07 192.234 166.247 176.084 192.084 193.381 186.24 180.327 171.373 219.463 196.11 193.915 209.559 186.222 177.953 165.319 193.874 177.855 203.791 201.6 160.228 170.67 197.765 195.806 192.875 191.188 185.416 208.104 198.519 199.615 174.177 206.2
180.387 202.49 165.695 168.421 190.383 185.074 181.21 182.2 177.038 223.031 194.368 195.058 206.923 180.143 185.717 165.61 193.263 183.27 202.333 193.131 167.563 169.129 184.823 195.908 187.97 187.359 173.744 210.02 190.338 196.713 173.235 196.476
200.028 222.379 183.416 176.032 201.347 199.943 204.247 192.192 188.126 234.979 208.584 211.417 219.776 194.05 192.39 182.363 211.865 193.565 216.488 204.918 183.33 187.699 212.799 211.238 198.045 210.722 197.794 225.336 214.778 221.08 188.225 215.426
205.26 225.686 196.78 187.549 220.234 204.348 207.028 205.419 197.1 241.064 217.916 217.097 225.827 197.739 200.353 185.297 219.462 200.918 227.148 214.596 190.675 203.241 218.399 225.154 214.431 223.114 205.853 235.222 225.984 220.549 207.357 233.589
203.498 220.651 171.896 179.694 201.655 203.431 203.493 195.833 190.442 239.29 205.767 214.263 224.726 192.583 193.532 172.471 208.589 187.98 222.218 210.134 175.749 179.197 199.773 209.196 199.797 192.783 193.871 215.499 215.451 212.242 192.771 213.796
182.526 209.546 177.849 175.428 204.273 182.368 191.479 180.596 182.262 223.603 194.781 211.557 206 188.095 190.005 160.001 192.327 191.792 212.591 190.925 168.204 179.481 202.149 206.505 190.341 191.001 181.683 220.306 210.875 206.847 181.879 208.124
212.823 230.737 205.755 200.703 220.843 218.087 217.06 209.802 204.025 249.05 229.278 230.853 241.04 217.16 209.308 202.3 229.972 210.312 232.978 215.704 198.376 199.466 216.106 225.787 218.206 227.091 212.989 245.84 228.153 230.581 207.966 233.971
187.105 196.656 175.569 179.248 197.028 197.193 182.429 182.386 181.516 216.889 196.383 200.538 207.365 188.45 179.691 170.777 201.793 184.883 209.883 201.579 173.517 172.366 197.378 208.363 189.461 202.229 182.667 212.128 203.559 201.316 187.944 205.962
218.762 235.105 201.672 202.297 224.894 216.117 216.972 212.857 210.716 257.158 228.378 233.688 242.736 216.186 213.482 200.621 230.715 213.161 239.91 227.32 199.074 204.904 233.041 239.874 224.288 223.809 211.437 252.189 240.161 231.981 211.852 241.224
191.398 212.493 177.042 180.303 199.451 204.715 201.684 189.119 178.474 231.143 209.057 207.782 221.268 189.961 194.033 178.03 204.157 187.391 208.217 206.443 176.936 184.282 195.469 203.993 202.809 196.413 193.669 222.932 205.164 211.515 192.434 215.261
210.154 218.79 197.1 196.841 213.699 209.003 202.269 201.905 198.587 243.187 221.013 219.092 231.759 206.885 193.449 195.215 219.785 205.853 228.668 210.028 194.537 195.006 208.008 225.126 207.38 213.444 197.122 235.521 217.444 227.819 200.947 223.776
188.549 201.308 177.477 175.847 190.975 201.693 186.362 185.01 187.259 228.703 202.155 198.669 216.716 191.498 179.778 174.501 195.422 182.606 204.956 201.457 177.967 176.055 196.968 203.542 194.4 195.407 181.259 212.66 206.951 211.06 183.412 205.225
203.474 222.551 194.631 185.676 208.147 205.509 198.639 193.609 184.348 231.976 214.68 216.798 222.133 194.336 196.152 185.363 210.439 191.949 211.162 198.943 186.145 195.633 203.934 218.837 201.682 213.538 200.186 225.837 219.244 211.199 199.327 218.32
196.577 221.915 188.102 182.115 207.84 212.357 193.563 188.923 189.783 232.938 213.35 211.575 224.601 190.545 193.071 178.277 195.044 201.072 217.507 208.602 182.467 189.589 210.918 210.596 206.544 210.417 200.097 227.372 213.878 217.41 193.405 211.581
194.101 210.034 174.308 181.335 202.396 203.555 194.861 198.079 181.119 224.221 205.644 209.732 218.163 195.4 193.428 178.159 201.074 182.127 215.319 208.812 173.998 186.667 195.973 211.351 193.732 203.977 193.971 214.849 209.591 211.414 185.598 214.325
199.252 221.144 189.692 181.594 205.358 208.175 193.56 191.848 191.218 237.279 217.932 208.927 225.636 189.564 190.837 180.72 208.488 191.892 218.667 212.814 187.81 184.439 205.605 209.556 209.544 209.523 193.066 227.039 216.923 212.96 200.491 216.781
205.105 226.135 191.186 188.842 216.71 224.724 204.774 207.755 195.138 250.464 225.587 225.842 237.969 205.375 197.263 187.589 210.483 203.719 227.997 217.691 188.604 205.453 208.959 226.528 218.28 208.747 207.655 239.152 220.647 227.723 195.56 233.354
190.773 207.621 182.108 178.257 199.823 200.55 195.082 180.783 183.591 225.773 204.31 212.367 213.234 189.451 189.965 170.923 201.922 187.361 212.518 195.93 176.382 180.857 197.921 205.729 197.589 192.217 184.99 223.649 208.255 207.076 191.582 212.956
210.833 230.065 200.858 185.942 225.153 210.009 213.586 209.818 208.075 258.518 223.591 231.787 238.114 208.592 205.845 189.768 229.613 208.79 230.599 215.018 196.424 200.156 222.178 232.317 217.088 222.63 206.384 240.566 233.301 224.879 205.425 239.504
192.78 213.246 180.055 168.917 207.861 202.116 196.545 188.988 185.644 227.109 207.976 219.363 218.73 195.476 195.825 179.047 211.87 198.282 217.419 199.53 182.172 182.517 199.339 212.567 190.526 197.928 193.762 223.545 209.286 215.665 194.396 214.858
202.842 213.661 175.784 176.777 196.367 202.675 200.059 187.597 187.988 238.986 209.376 211.238 216.019 192.608 195.201 176.942 209.477 189.806 209.54 205.905 176.752 188.463 205.342 207.566 197.238 194.674 187.877 217.414 215.835 208.59 191.848 211.038
185.874 205.197 183.1 168.016 193.208 191.746 188.469 183.703 180.503 223.899 209.203 197.391 210.933 181.65 179.992 173.743 200.186 181.385 200.187 188.488 182.685 179.814 192.017 204.149 196.524 196.718 181.533 216.24 203.33 204.669 191.397 208.458
209.472 231.803 208.184 189.398 219.385 216.307 216.163 200.286 201.594 253.226 234.095 228.77 240.52 206.614 210.184 196.927 221.101 211.017 221.284 217.528 200.345 202.062 229.987 228.87 222.345 221.615 213.616 250.681 231.445 231.413 211.373 228.294
189.864 214.778 180.913 180.861 194.651 209.685 196.745 190.695 180.169 231.477 207.895 205.603 212.693 192.307 188.316 180.774 203.569 190.98 209.996 196.045 179.024 185.962 191.383 204.705 197.38 202.96 188.002 223.732 197.405 210.395 179.795 209.216

Upvotes: 1

Related Questions