cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

empol
Journeyman III

No output from kernel

I made some GUI app, which takes a picture (BMP) and just copies it over kernel. Works fine, but just only for some resolutions. it works for 600x450 but doesn't for 800x600.

Second problem: when i modified kernel to modify image it also has no output. Small modification - adding some value (under 255) for every color of pixel. Tested on black image (every pixel is RGB(0,0,0)).

So it works only for specific resolutions and only copy data.

 

With debugger i found that there is notrhing in the output buffer.

There is allocated pixelarray (array of uchar4) for output data, but after clEnqueueReadBuffer pixelarray length is 0.

I think it's kernel problem, but can't find what's wrong. The same situation with CPU and GPU.

kernel before modification (works for some resolutions): __kernel void hello(__global uchar4 * in, __global uchar4 * out) { unsigned int sizeX = get_global_size(0); unsigned int sizey = get_global_size(1); unsigned int x = get_global_id(0); unsigned int y = get_global_id(1); uint Index = y + x * sizey; out[Index] = in[Index]; } after modification (doesn't work at all): __kernel void hello(__global uchar4 * in, __global uchar4 * out) { unsigned int sizeX = get_global_size(0); unsigned int sizey = get_global_size(1); unsigned int x = get_global_id(0); unsigned int y = get_global_id(1); uchar4 mod = (uchar4)(50,50,80,0); uint Index = y + x * sizey; out[Index] = in[Index] + mod; } Functions to copy buffers an run kernel: private: void writeBuffer(cl_command_queue commandQueue, cl_mem inCL, System::Drawing::Image^ image) { cl_int err; array<unsigned char>^ chararray = ConvertImageToCharArray(image); ImgBuf^ imgbuf = CharArrayToPixelArray(chararray, image->Width, image->Height); pixelArray^ pa = imgbuf->pa; err = clEnqueueWriteBuffer( commandQueue, inCL, 1, 0, sizeof(pa), &pa, 0, 0, 0); checkErr(err, "clEnqueueWriteBuffer failed"); } private: System::Drawing::Image^ readBuffer(cl_command_queue commandQueue, cl_mem outCL) { cl_event events; cl_int err; pixelArray^ parr = gcnew pixelArray(imgi->Width * imgi->Height); System::Drawing::Image^ image; array<unsigned char>^ chararray; err = clEnqueueReadBuffer( commandQueue, outCL, 1, 0, sizeof(parr), &parr, 0, 0, &events); checkErr(err, "clEnqueueReadBuffer failed"); err = clWaitForEvents(1, &events); checkErr(err, "clWaitForEvents failed"); chararray = PixelArrayToCharArray(parr, lg); image = ConvertCharArrayToImage(chararray); return image; } private: void runKernel(cl_command_queue commandQueue, cl_kernel kernel) { cl_int err; cl_event events; cl_int width = imgi->Width; cl_int height = imgi->Height; size_t global[] = {width, height}; size_t local[] = {1,1}; err = clEnqueueNDRangeKernel( commandQueue, kernel, 2, NULL, global, local, 0, NULL, &events); checkErr(err, "clEnqueueNDRangeKernel failed"); err = clWaitForEvents(1, &events); checkErr(err, "clWaitForEvents failed"); }

0 Likes
7 Replies
genaganna
Journeyman III

Originally posted by: empol I made some GUI app, which takes a picture (BMP) and just copies it over kernel. Works fine, but just only for some resolutions. it works for 600x450 but doesn't for 800x600.

 

Second problem: when i modified kernel to modify image it also has no output. Small modification - adding some value (under 255) for every color of pixel. Tested on black image (every pixel is RGB(0,0,0)).

 

So it works only for specific resolutions and only copy data.

 

 

 

With debugger i found that there is notrhing in the output buffer.

 

There is allocated pixelarray (array of uchar4) for output data, but after clEnqueueReadBuffer pixelarray length is 0.

 

I think it's kernel problem, but can't find what's wrong. The same situation with CPU and GPU.

 

Please add following statement in your kernel and see you are getting correct results or not.

       #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable

 

I am not sure what you are indented to do in kernel.   could you please replace following statement from kernel

         uint Index = y + x * sizey;

                       with

         uint Index = y * sizex + x;

0 Likes

Tried both. It didn't help. Still the same.

 

I got a few warnings:

warning LNK4248: unresolved typeref token (0100000F) for '_cl_mem'; image may not run

 

Maybe it's a problem. And if it is how to fix it?

 

Another test. After process 600x450 i can run several times other resolutions (with one instance). It works. But after few images crash again.

0 Likes

Originally posted by: empol Tried both. It didn't help. Still the same.

 

 I got a few warnings:

 

warning LNK4248: unresolved typeref token (0100000F) for '_cl_mem'; image may not run

 

 Maybe it's a problem. And if it is how to fix it?

 

 Another test. After process 600x450 i can run several times other resolutions (with one instance). It works. But after few images crash again.

 

Please paste your kernel code and host code here which helps use to give quick reply.  Please also give following details.

     OS, GPU, CPU, SDK version and Driver version.

0 Likes

OS: Windwos 7 x64

GPU: HD4850

CPU: C2D E7200 @ 3,8GHz (full stable tested with prime95 for hours)

SDK: 2.1

Driver 10.4

 

To test image processing change

    uchar4 mod = (uchar4)(0,0,0,0);

    uint Index = y * sizeX + x;

   out[Index] = in[Index] + mod;

 

to:

    uchar4 mod = (uchar4)(255,255,255,0);

    uint Index = y * sizeX + x;

   out[Index] = mod - in[Index];

1.cl #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable __kernel void hello(__global uchar4 * in, __global uchar4 * out) { unsigned int sizeX = get_global_size(0); unsigned int sizeY = get_global_size(1); unsigned int x = get_global_id(0); unsigned int y = get_global_id(1); uchar4 mod = (uchar4)(0,0,0,0); uint Index = y * sizeX + x; out[Index] = in[Index] + mod; } stdafx.h // stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently #pragma once // TODO: reference additional headers your program requires here #define __NO_STD_VECTOR // Use cl::vector and cl::string and #define __NO_STD_STRING // not STL versions, more on this later #include <malloc.h> #define alloca _alloca #include <CL\cl.h> #include <fstream> #include <iostream> #include <string> #include <iterator> value struct pixel { unsigned char r; unsigned char g; unsigned char b; unsigned char w; }; typedef array<pixel> pixelArray; value struct ImgBuf { array<char>^ header; pixelArray^ pa; }; value struct OCLBuf { cl_mem inCL; cl_mem outCL; } ; OCLBuf oclbuf(size_t size_in, size_t size_out); inline void checkErr(cl_int err, const char * name) { if (err != CL_SUCCESS) { System::String^ name1 = gcnew System::String(name); System::Windows::Forms::MessageBox::Show("OpenCL error: " + name1 + " " + err, "B??D", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error); //system("PAUSE"); //exit(EXIT_FAILURE); } } form1.h #pragma once namespace mgr { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::GroupBox^ groupBox1; private: System::Windows::Forms::MenuStrip^ menuStrip1; private: System::Windows::Forms::ToolStripMenuItem^ plikToolStripMenuItem; private: System::Windows::Forms::ToolStripMenuItem^ otwórzToolStripMenuItem; private: System::Windows::Forms::ToolStripMenuItem^ zapiszToolStripMenuItem; private: System::Windows::Forms::ToolStripMenuItem^ oProgramieToolStripMenuItem; private: System::Windows::Forms::ToolStripMenuItem^ autorToolStripMenuItem; private: System::Windows::Forms::RadioButton^ oclgpu_rb; private: System::Windows::Forms::RadioButton^ oclcpu_rb; private: System::Windows::Forms::RadioButton^ serial_rb; private: System::Windows::Forms::PictureBox^ imgin; private: System::Windows::Forms::PictureBox^ imgout; private: System::Drawing::Image^ imgi; private: System::Drawing::Image^ imgo; cl_context context; //cl_mem inCL, outCL; //size_t deviceListSize; //cl_device_id *devices; cl_program program; cl_kernel kernel; cl_command_queue queue; array<char>^ header; OCLBuf^ buffer; protected: private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->groupBox1 = (gcnew System::Windows::Forms::GroupBox()); this->oclgpu_rb = (gcnew System::Windows::Forms::RadioButton()); this->oclcpu_rb = (gcnew System::Windows::Forms::RadioButton()); this->serial_rb = (gcnew System::Windows::Forms::RadioButton()); this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip()); this->plikToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->otwórzToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->zapiszToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->oProgramieToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->autorToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->imgin = (gcnew System::Windows::Forms::PictureBox()); this->imgout = (gcnew System::Windows::Forms::PictureBox()); this->groupBox1->SuspendLayout(); this->menuStrip1->SuspendLayout(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->imgin))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->imgout))->BeginInit(); this->SuspendLayout(); // // groupBox1 // this->groupBox1->Controls->Add(this->oclgpu_rb); this->groupBox1->Controls->Add(this->oclcpu_rb); this->groupBox1->Controls->Add(this->serial_rb); this->groupBox1->Location = System::Drawing::Point(12, 27); this->groupBox1->Name = L"groupBox1"; this->groupBox1->Size = System::Drawing::Size(120, 100); this->groupBox1->TabIndex = 0; this->groupBox1->TabStop = false; this->groupBox1->Text = L"Urz?dzenie"; // // oclgpu_rb // this->oclgpu_rb->AutoSize = true; this->oclgpu_rb->Location = System::Drawing::Point(7, 68); this->oclgpu_rb->Name = L"oclgpu_rb"; this->oclgpu_rb->Size = System::Drawing::Size(90, 17); this->oclgpu_rb->TabIndex = 2; this->oclgpu_rb->TabStop = true; this->oclgpu_rb->Text = L"OpenCL GPU"; this->oclgpu_rb->UseVisualStyleBackColor = true; // // oclcpu_rb // this->oclcpu_rb->AutoSize = true; this->oclcpu_rb->Location = System::Drawing::Point(7, 44); this->oclcpu_rb->Name = L"oclcpu_rb"; this->oclcpu_rb->Size = System::Drawing::Size(89, 17); this->oclcpu_rb->TabIndex = 1; this->oclcpu_rb->TabStop = true; this->oclcpu_rb->Text = L"OpenCL CPU"; this->oclcpu_rb->UseVisualStyleBackColor = true; // // serial_rb // this->serial_rb->AutoSize = true; this->serial_rb->Location = System::Drawing::Point(7, 20); this->serial_rb->Name = L"serial_rb"; this->serial_rb->Size = System::Drawing::Size(78, 17); this->serial_rb->TabIndex = 0; this->serial_rb->TabStop = true; this->serial_rb->Text = L"Szeregowo"; this->serial_rb->UseVisualStyleBackColor = true; // // menuStrip1 // this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->plikToolStripMenuItem, this->oProgramieToolStripMenuItem}); this->menuStrip1->Location = System::Drawing::Point(0, 0); this->menuStrip1->Name = L"menuStrip1"; this->menuStrip1->Size = System::Drawing::Size(1079, 24); this->menuStrip1->TabIndex = 1; this->menuStrip1->Text = L"menuStrip1"; // // plikToolStripMenuItem // this->plikToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->otwórzToolStripMenuItem, this->zapiszToolStripMenuItem}); this->plikToolStripMenuItem->Name = L"plikToolStripMenuItem"; this->plikToolStripMenuItem->Size = System::Drawing::Size(38, 20); this->plikToolStripMenuItem->Text = L"Plik"; // // otwórzToolStripMenuItem // this->otwórzToolStripMenuItem->Name = L"otwórzToolStripMenuItem"; this->otwórzToolStripMenuItem->Size = System::Drawing::Size(112, 22); this->otwórzToolStripMenuItem->Text = L"Otwórz"; this->otwórzToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::otwórzToolStripMenuItem_Click); // // zapiszToolStripMenuItem // this->zapiszToolStripMenuItem->Name = L"zapiszToolStripMenuItem"; this->zapiszToolStripMenuItem->Size = System::Drawing::Size(112, 22); this->zapiszToolStripMenuItem->Text = L"Zapisz"; this->zapiszToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::zapiszToolStripMenuItem_Click); // // oProgramieToolStripMenuItem // this->oProgramieToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->autorToolStripMenuItem}); this->oProgramieToolStripMenuItem->Name = L"oProgramieToolStripMenuItem"; this->oProgramieToolStripMenuItem->Size = System::Drawing::Size(86, 20); this->oProgramieToolStripMenuItem->Text = L"O programie"; // // autorToolStripMenuItem // this->autorToolStripMenuItem->Name = L"autorToolStripMenuItem"; this->autorToolStripMenuItem->Size = System::Drawing::Size(104, 22); this->autorToolStripMenuItem->Text = L"Autor"; // // imgin // this->imgin->Location = System::Drawing::Point(19, 145); this->imgin->Name = L"imgin"; this->imgin->Size = System::Drawing::Size(470, 342); this->imgin->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->imgin->TabIndex = 2; this->imgin->TabStop = false; // // imgout // this->imgout->Location = System::Drawing::Point(496, 145); this->imgout->Name = L"imgout"; this->imgout->Size = System::Drawing::Size(528, 342); this->imgout->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->imgout->TabIndex = 3; this->imgout->TabStop = false; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(1079, 516); this->Controls->Add(this->imgout); this->Controls->Add(this->imgin); this->Controls->Add(this->groupBox1); this->Controls->Add(this->menuStrip1); this->MainMenuStrip = this->menuStrip1; this->Name = L"Form1"; this->Text = L"Filtrowanie obrazów"; this->groupBox1->ResumeLayout(false); this->groupBox1->PerformLayout(); this->menuStrip1->ResumeLayout(false); this->menuStrip1->PerformLayout(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->imgin))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->imgout))->EndInit(); this->ResumeLayout(false); this->PerformLayout(); } private: array<unsigned char>^ ConvertImageToCharArray(System::Drawing::Image^ imageToConvert) { array<unsigned char>^ Ret; System::IO::MemoryStream^ mst = gcnew System::IO::MemoryStream(); imageToConvert->Save(mst,System::Drawing::Imaging::ImageFormat::Bmp); Ret = mst->ToArray(); return Ret; } private: System::Drawing::Image^ ConvertCharArrayToImage(array<unsigned char>^ arrayToConvert) { System::Drawing::Image^ Ret; System::IO::MemoryStream^ mst = gcnew System::IO::MemoryStream(); mst->Write(arrayToConvert, 0, arrayToConvert->Length); Ret = System::Drawing::Image::FromStream(mst,true); return Ret; } private: ImgBuf^ CharArrayToPixelArray(array<unsigned char>^ arrayToConvert, int width, int height) { ImgBuf ^imgbuf = gcnew ImgBuf(); imgbuf->header = gcnew array<char>(54); imgbuf->pa = gcnew pixelArray(width * height + (height * (4 - (3 * width) % 4) % 4)); for(int i = 0; i < 54; i++) { imgbuf->header = arrayToConvert; } header = imgbuf->header; unsigned int index = 54; for(int y = 0; y < height - 1; y++) { for(int x = 0; x < width; x++) { imgbuf->pa[(y * width + x)].b = arrayToConvert[index++]; imgbuf->pa[(y * width + x)].g = arrayToConvert[index++]; imgbuf->pa[(y * width + x)].r = arrayToConvert[index++]; } for(int x = 0; x < (4 - (3 * width) % 4) % 4; x++) { index++; } } for(int x = 0; x < width; x++) { imgbuf->pa[((height - 1) * width + x)].b = arrayToConvert[index++]; imgbuf->pa[((height - 1) * width + x)].g = arrayToConvert[index++]; imgbuf->pa[((height - 1) * width + x)].r = arrayToConvert[index++]; } return imgbuf; } private: array<unsigned char>^ PixelArrayToCharArray(pixelArray^ pa, unsigned int length) { ImgBuf^ imgbuf = gcnew ImgBuf(); imgbuf->pa = pa; array<unsigned char>^ Ret = gcnew array<unsigned char>(length * 3 + 54); unsigned int index = 54, temp = imgbuf->pa->Length - 1; imgbuf->header = header; for(int i = 0; i < 54; i++) { Ret = imgbuf->header; } for(int i = 0; i < imgbuf->pa->Length - 1; i++) { Ret[index++] = imgbuf->pa.b; Ret[index++] = imgbuf->pa.g; Ret[index++] = imgbuf->pa.r; } Ret[index++] = imgbuf->pa[temp].b; Ret[index++] = imgbuf->pa[temp].g; Ret[index++] = imgbuf->pa[temp].r; return Ret; } private: cl_context initContext(cl_device_type dType) { cl_context context; cl_int err; cl_uint numPlatforms; if(oclcpu_rb->Checked) { dType = CL_DEVICE_TYPE_CPU; } else if(oclgpu_rb->Checked) { dType = CL_DEVICE_TYPE_CPU; } cl_platform_id platform = NULL; err = clGetPlatformIDs(0, NULL, &numPlatforms); checkErr(err, "cl::Platform::get"); if (0 < numPlatforms) { cl_platform_id* platforms = new cl_platform_id[numPlatforms]; err = clGetPlatformIDs(numPlatforms, platforms, NULL); checkErr(err, "cl::Platform::get"); for (unsigned i = 0; i < numPlatforms; ++i) { char pbuf[100]; err = clGetPlatformInfo(platforms, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL); platform = platforms; if (!strcmp(pbuf, "Advanced Micro Devices, Inc.")) { break; } } delete[] platforms; } cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 }; /* Use NULL for backward compatibility */ cl_context_properties* cprops = (NULL == platform) ? NULL : cps; context = clCreateContextFromType(cprops, dType, NULL, NULL, &err); checkErr(err, "clCreateContextFromType failed"); return context; } private: cl_device_id* devices() { cl_int err; size_t deviceListSize; cl_device_id *devices; err = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &deviceListSize); checkErr(err, "clGetContextInfo failed"); devices = (cl_device_id*)malloc(deviceListSize); if(devices==NULL) { std::cerr << "ERROR: " << std::endl; exit(EXIT_FAILURE); } err = clGetContextInfo( context, CL_CONTEXT_DEVICES, deviceListSize, devices, NULL); checkErr(err, "clGetContextInfo failed"); return devices; } private: cl_program initProgram(cl_context context, cl_device_id* devices, const char * kernlpath) { cl_program program; cl_int err; std::ifstream file(kernlpath); checkErr(file.is_open() ? CL_SUCCESS:-1, kernlpath); std::string source( std::istreambuf_iterator<char>(file), (std::istreambuf_iterator<char>())); const char* src; src = source.c_str(); //printf("%s",src); size_t sourceSize[] = {strlen(src)}; program = clCreateProgramWithSource(context, 1, &src, sourceSize, &err); checkErr(err, "clGetContextInfo failed"); err = clBuildProgram(program, 1, devices, NULL, NULL, NULL); if(err != CL_SUCCESS) { if(err == CL_BUILD_PROGRAM_FAILURE) { cl_int logStatus; char *buildLog = NULL; size_t buildLogSize = 0; logStatus = clGetProgramBuildInfo (program, devices[0], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, &buildLogSize); checkErr(err, "GetProgramBuildInfo failed"); buildLog = (char*)malloc(buildLogSize); if(buildLog == NULL) { std::cout << "Failed to allocate host memory. (buildLog)"; exit(EXIT_FAILURE); } memset(buildLog, 0, buildLogSize); logStatus = clGetProgramBuildInfo (program, devices[0], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, NULL); checkErr(err, "clGetProgramBuildInfo"); { free(buildLog); exit(EXIT_FAILURE); } std::cout << " \n\t\t\tBUILD LOG\n"; std::cout << " ************************************************\n"; std::cout << buildLog << std::endl; std::cout << " ************************************************\n"; free(buildLog); } checkErr(err, "clBuildProgram failed"); } return program; } private: cl_kernel initKernel(cl_program program, const char * kernelname) { cl_int err; cl_kernel kernel; kernel = clCreateKernel(program, "hello", &err); checkErr(err, "clCreateKernel failed"); return kernel; } private: OCLBuf oclbuf(size_t size_in, size_t size_out) { OCLBuf oclbuf; cl_int err; oclbuf.inCL = clCreateBuffer( context, CL_MEM_READ_ONLY, size_in, NULL, &err); checkErr(err, "clCreateBuffer failed"); oclbuf.outCL = clCreateBuffer( context, CL_MEM_WRITE_ONLY, size_out, NULL, &err); checkErr(err, "clCreateBuffer failed"); return oclbuf; } private: void setKernelArgs(cl_kernel kernel, cl_mem inCL, cl_mem outCL) { cl_int err; err = clSetKernelArg(kernel, 0, sizeof(inCL), &inCL); checkErr(err, "clSetKernelArg"); err = clSetKernelArg(kernel, 1, sizeof(outCL), &outCL); checkErr(err, "clSetKernelArg"); } private: cl_command_queue commandQueue(cl_context context, cl_device_id* devices) { cl_int err; cl_command_queue commandQueue; cl_command_queue_properties prop = 0; prop = CL_QUEUE_PROFILING_ENABLE; commandQueue = clCreateCommandQueue( context, devices[0], prop, &err); checkErr(err, "clCreateCommandQueue failed"); return commandQueue; } private: void writeBuffer(cl_command_queue commandQueue, cl_mem inCL, System::Drawing::Image^ image) { cl_int err; array<unsigned char>^ chararray = ConvertImageToCharArray(image); ImgBuf^ imgbuf = CharArrayToPixelArray(chararray, image->Width, image->Height); pixelArray^ pa = imgbuf->pa; err = clEnqueueWriteBuffer( commandQueue, inCL, 1, 0, sizeof(pa), &pa, 0, 0, 0); checkErr(err, "clEnqueueWriteBuffer failed"); } private: System::Drawing::Image^ readBuffer(cl_command_queue commandQueue, cl_mem outCL) { cl_event events; cl_int err; pixelArray^ parr = gcnew pixelArray(imgi->Width * imgi->Height); System::Drawing::Image^ image; array<unsigned char>^ chararray; err = clEnqueueReadBuffer( commandQueue, outCL, 1, 0, sizeof(parr), &parr, 0, 0, 0); //checkErr(err, "clEnqueueReadBuffer failed"); //err = clWaitForEvents(1, &events); checkErr(err, "clWaitForEvents failed"); chararray = PixelArrayToCharArray(parr, parr->Length); image = ConvertCharArrayToImage(chararray); return image; } private: void runKernel(cl_command_queue commandQueue, cl_kernel kernel) { cl_int err; cl_event events; cl_int width = imgi->Width; cl_int height = imgi->Height; size_t global[] = {width, height}; size_t local[] = {1,1}; err = clEnqueueNDRangeKernel( commandQueue, kernel, 2, NULL, global, NULL, //local, 0, NULL, &events); checkErr(err, "clEnqueueNDRangeKernel failed"); err = clWaitForEvents(1, &events); checkErr(err, "clWaitForEvents failed"); } private: void cleanCL() { cl_int err; err = clReleaseKernel(kernel); checkErr(err, "clReleaseKernel failed"); err = clReleaseProgram(program); checkErr(err, "clReleaseProgram failed"); err = clReleaseMemObject(buffer->inCL); checkErr(err, "clReleaseMemObject failed"); err = clReleaseMemObject(buffer->outCL); checkErr(err, "clReleaseMemObject failed"); err = clReleaseCommandQueue(queue); checkErr(err, "clReleaseCommandQueuel failed"); err = clReleaseContext(context); checkErr(err, "clReleaseContext failed"); } #pragma endregion private: System::Void otwórzToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; openFileDialog1->Filter = "Bitmapa BMP (*.bmp)|*.bmp|All files (*.*)|*.*"; if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ) { fclose(stdout); fclose(stderr); imgi = Image::FromFile(openFileDialog1->FileName); if((imgi->Width % 4 == 0) || (imgi->Width % 4 == 0)) { imgin->Image = imgi; cl_device_type dType = CL_DEVICE_TYPE_CPU; context = initContext(dType); program = initProgram(context, devices(), "1.cl"); kernel = initKernel(program, "hello"); buffer = oclbuf(sizeof(pixel) * imgi->Width * imgi->Height, sizeof(pixel) * imgi->Width * imgi->Height); setKernelArgs(kernel, buffer->inCL, buffer->outCL); queue = commandQueue(context, devices()); writeBuffer(queue, buffer->inCL, imgi); for(int i = 0; i < 1; i++) { runKernel(queue, kernel); } imgo = readBuffer(queue, buffer->outCL); cleanCL(); imgout->Image = imgo; } else { System::Windows::Forms::MessageBox::Show("rozdzielczo?? przez 4"); } } } private: System::Void zapiszToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { //Stream^ myStream; SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog; saveFileDialog1->Filter = "bmp files (*.bmp)|*.bmp|All files (*.*)|*.*"; if ( saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ) { imgo->Save(saveFileDialog1->FileName, System::Drawing::Imaging::ImageFormat::Bmp); } } }; }

0 Likes

http://empol.x.pl/vid/mgr.rar

Here is my project. May be easier to check working code.

0 Likes

Can anybody help me?

It's important for me.

 

When i set blocking to 0 in clEnqueueReadBuffer uotput buffer has correct size, but output image is black.

0 Likes

I changed arrays to pointers. Now everything is perfect.

0 Likes