0

I took a matrix multiplication written using OpenCL from here:

src = "__kernel void
floatMatrixMult(__global float * MResp,
                __global float * M1,
                __global float * M2,
                __global int * q)
   {
    // Vector element index
    int i = get_global_id(0);
    int j = get_global_id(1);
    int p = get_global_size(0);
    int r = get_global_size(1);
    MResp[i + p * j] = 0;
    int QQ = q[0];
    for (int k = 0; k < QQ; k++)
    {
        MResp[i + p * j] += M1[i + p * k] * M2[k + QQ * j];
    }
}";

then I used:

test = OpenCLFunctionLoad[{src}, "floatMatrixMult", {"Float", "Float", "Float", _Integer}, {8, 8}];

but I get this error:

OpenCLFunctionLoad::nofile: -- Message text not found -- (contents of src) >>

There isn't any documents to explain this. How can I resolve the issue? If the code itself has problem are there some other codes to do matrix multiplication using OpenCL?

Oleksandr R.
  • 23,023
  • 4
  • 87
  • 125
MOON
  • 3,864
  • 23
  • 49

1 Answers1

2
Needs["OpenCLLink`"]

test = OpenCLFunctionLoad[src, "floatMatrixMult", {
    {"Float", 2, "Output"},
    {"Float", 2, "Input"}, {"Float", 2, "Input"}, {_Integer, 1, "Input"}
   },
   {16, 16}
  ];

SIZE = 1024;

a = RandomReal[1, {SIZE, SIZE}];
m1 = OpenCLMemoryLoad[a, "Float"];

b = RandomReal[10, {SIZE, SIZE}];
m2 = OpenCLMemoryLoad[b, "Float"];

out = OpenCLMemoryAllocate["Float", {SIZE, SIZE}];

test[out, m2, m1, {SIZE, SIZE}];

Norm[OpenCLMemoryGet[out] - a.b]
(* -> 0.0809831 *)
Oleksandr R.
  • 23,023
  • 4
  • 87
  • 125