AnsweredAssumed Answered

Problem with selection sort ( using C#.Net and OpenCL )

Question asked by bz7 on Jun 1, 2013
Latest reply on Jun 3, 2013 by himanshu.gautam

Hi everyone

I'm a somewhat noob in programming and completely new to OpenCL !

I wrote some OpenCL code inside a C# Project ( using some online guide ) . its a selection sort but the problem is it won't sort at all ! it returns the number without any order

I really don't know what I did wrong so help me out here

this is the OpenCL code :

__kernel void ParallelSelection(__global int * in1)
  {
  int i = get_global_id(0); // current thread
  int n = get_global_size(0); // input size
  int temp;
  for (int j = i + 1; j < n; j++)
  {
  if (in1[j] < in1[i])
  {
  temp = in1[j];
  in1[j] = in1[i];
  in1[i] = temp;
  }
  }
  }



 

 

and this is the C# code :

* vecSum  is the string containing OpenCL code

 

          //Initializes OpenCL Platforms and Devices and sets everything up
            OpenCLTemplate.CLCalc.InitCL();

            //Compiles the source codes. The source is a string array because the user may want
            //to split the source into many strings.
            OpenCLTemplate.CLCalc.Program.Compile(new string[] { vecSum });

            //Gets host access to the OpenCL floatVectorSum kernel
            OpenCLTemplate.CLCalc.Program.Kernel VectorSum = new OpenCLTemplate.CLCalc.Program.Kernel("ParallelSelection");

            //////////////////////

           //numbers
            int n = 9;

            //Create vectors with 2000 numbers
            int[] v1 = new int[9]{3,14,4,6,-45,4,23,5,1};
        
            ////////////////////////

            //Creates vectors v1 in the device memory
            OpenCLTemplate.CLCalc.Program.Variable varV1 = new OpenCLTemplate.CLCalc.Program.Variable(v1);
        
            ////////////////////////

            //Arguments of VectorSum kernel
            OpenCLTemplate.CLCalc.Program.Variable[] args = new OpenCLTemplate.CLCalc.Program.Variable[] { varV1 };

            //How many workers will there be? We need "n", one for each element
            int[] workers = new int[1] { n };

            //Execute the kernel

            VectorSum.Execute(args, workers);

            ////////////////////////////

            //Read device memory varV1 to host memory v1

            varV1.ReadFromDeviceTo(v1);
            for (int i = 0; i < n; i++)
            {
                listBox1.Items.Add(v1[i].ToString());

            }



 

__________________________________

 

EDIT : Thanks everyone I finally getting to know the SIMD model , for other people like myself interested in a simple sorting algorithm ( parallel of course ) here's the parallel selection sort OpenCL kernel :

 



__kernel void ParallelSelection(__global int * in1,__global int * out)
                {
                  int i = get_global_id(0); // current thread
                  int n = get_global_size(0); // input size
                    int ikey = in1[i];
                    int pos = 0;
                  for (int j = 0 ; j < n; j++)
                    {
                                    int jkey = in1[j];
                                    bool smaller = (jkey < ikey);
                                    pos+= (smaller)?1:0;
                                    
                    }
                            out[pos]=ikey;

                    }

 

Message was edited by: Behrooz Moradi

Outcomes