ViennaCL - The Vienna Computing Library  1.5.2
viennacl/device_specific/generate.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_DEVICE_SPECIFIC_GENERATE_HPP
00002 #define VIENNACL_DEVICE_SPECIFIC_GENERATE_HPP
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2013, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00021 
00026 #include <cstring>
00027 #include <vector>
00028 #include <typeinfo>
00029 
00030 #include "viennacl/scheduler/forwards.h"
00031 
00032 #include "viennacl/device_specific/forwards.h"
00033 #include "viennacl/device_specific/templates/template_base.hpp"
00034 #include "viennacl/device_specific/tree_parsing/statement_representation.hpp"
00035 
00036 namespace viennacl{
00037 
00038   namespace device_specific{
00039 
00040     namespace generate{
00041 
00042 
00043       //CUDA Conversion
00044       inline std::string opencl_source_to_cuda_source(std::string const & opencl_src)
00045       {
00046         std::string res = opencl_src;
00047 
00048         viennacl::tools::find_and_replace(res,"__attribute__","//__attribute__");
00049 
00050         //Pointer
00051         viennacl::tools::find_and_replace(res, "__global float*", "float*");
00052         viennacl::tools::find_and_replace(res, "__local float*", "float*");
00053 
00054         viennacl::tools::find_and_replace(res, "__global double*", "double*");
00055         viennacl::tools::find_and_replace(res, "__local double*", "double*");
00056 
00057         //Qualifiers
00058         viennacl::tools::find_and_replace(res,"__global","__device__");
00059         viennacl::tools::find_and_replace(res,"__kernel","__global__");
00060         viennacl::tools::find_and_replace(res,"__constant","__constant__");
00061         viennacl::tools::find_and_replace(res,"__local","__shared__");
00062 
00063         //Indexing
00064         viennacl::tools::find_and_replace(res,"get_num_groups(0)","gridDim.x");
00065         viennacl::tools::find_and_replace(res,"get_num_groups(1)","gridDim.y");
00066 
00067         viennacl::tools::find_and_replace(res,"get_local_size(0)","blockDim.x");
00068         viennacl::tools::find_and_replace(res,"get_local_size(1)","blockDim.y");
00069 
00070         viennacl::tools::find_and_replace(res,"get_group_id(0)","blockIdx.x");
00071         viennacl::tools::find_and_replace(res,"get_group_id(1)","blockIdx.y");
00072 
00073         viennacl::tools::find_and_replace(res,"get_local_id(0)","threadIdx.x");
00074         viennacl::tools::find_and_replace(res,"get_local_id(1)","threadIdx.y");
00075 
00076         viennacl::tools::find_and_replace(res,"get_global_id(0)","(blockIdx.x*blockDim.x + threadIdx.x)");
00077         viennacl::tools::find_and_replace(res,"get_global_id(1)","(blockIdx.y*blockDim.y + threadIdx.y)");
00078 
00079         //Synchronization
00080         viennacl::tools::find_and_replace(res,"barrier(CLK_LOCAL_MEM_FENCE)","__syncthreads()");
00081         viennacl::tools::find_and_replace(res,"barrier(CLK_GLOBAL_MEM_FENCE)","__syncthreads()");
00082 
00083 
00084         return res;
00085       }
00086     }
00087 
00088   }
00089 }
00090 #endif