Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <36d24a69.2a2b9153.51d9cabb.b694@o2.pl>
Date: Sun, 07 Jul 2013 22:08:27 +0200
From: marcus.desto <marcus.desto@...pl>
To: john-dev@...ts.openwall.com
Subject: String pass to GPU and get back

Hi,

I have troubles with setting up a program to use some of your opencl implementations.
Therefore I try to understand how to pass a string to the GPU and get it back to the host program to print it.
I did it with a uint array without problems. But using uchar for passing a string I stuck.

OpenCL:
__kernel void same_in_same_out_char(__global uchar * out, __constant uchar * in){
  for (unsigned int ui=0; ui<3; ui++) out[ui]=in[ui];
}

C++
#define __CL_ENABLE_EXCEPTIONS

#include <fstream>
#include <iostream>
#include <iterator>
#include <CL/cl.hpp>
#include <CL/opencl.h>


using namespace std;
int main () {

    vector<cl::Platform> platforms;
    vector<cl::Device> devices;
    vector<cl::Kernel> kernels;
    
    try {
    
        // create platform
        cl::Platform::get(&platforms);
        platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &devices);

        // create context
        cl::Context context(devices);

        // create command queue
        cl::CommandQueue queue(context, devices[0]);

        // load opencl source
        ifstream cl_file("inout.cl");
        string cl_string(istreambuf_iterator<char>(cl_file), (istreambuf_iterator<char>()));
        cl::Program::Sources source(1, make_pair(cl_string.c_str(), 
            cl_string.length() + 1));

        // create program
        cl::Program program(context, source);

        // compile opencl source
        program.build(devices);

        // load named kernel from opencl source
        cl::Kernel kernel(program, "same_in_same_out_char");

        // create a message to send to kernel ----------------------------------------------------------------
        const char pwd[] = "MAX";
        cout << "char pwd[] : " << pwd << endl;
        cl_uchar * password = (cl_uchar*) &pwd;
        int bufferA_size = 3; // array size is 3

        int bufferC_size = 3; // array size is 3
        cout << " -- OpenCL -- " << endl;
        // ---------------------------------------------------------------------------------------------------
        // allocate device buffer to hold message
        cl::Buffer bufferA(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_uchar) * bufferA_size, password);
        cl::Buffer bufferC(context, CL_MEM_WRITE_ONLY, sizeof(cl_uchar) * bufferC_size);

        // set message as kernel argument
        kernel.setArg(0, bufferC);
        kernel.setArg(1, bufferA);

         // execute kernel
        queue.enqueueTask(kernel);

        // wait for completion
        queue.finish();
        // ----------------------

        cl_uint out_global[bufferC_size];
	queue.enqueueReadBuffer(bufferC, CL_TRUE, 0, bufferC_size*sizeof(cl_uchar), &out_global);

        cout << "Output \t\t:" << *out_global << endl << "Output[1..n] \t:";
        for (unsigned int i=0; i<bufferC_size; i ++) cout << out_global[i] << " " ;

        cout << endl;
    } catch (cl::Error e) {
        cout << endl << e.what() << " : " << e.err() << endl;
    }
    
    return 0;
    
}

Any ideas?

(Thanks)
Regards
Marcus

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.