2015-12-19 21:35:35 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, PHILIPPE TILLET. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of ISAAC.
|
|
|
|
*
|
|
|
|
* ISAAC is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
2015-12-21 17:04:09 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/driver/kernel.h"
|
|
|
|
#include "isaac/driver/buffer.h"
|
2016-04-02 18:19:33 -04:00
|
|
|
#include "isaac/value_scalar.h"
|
2015-04-29 15:50:57 -04:00
|
|
|
#include <iostream>
|
2015-08-20 21:24:41 -04:00
|
|
|
#include <cstring>
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
namespace isaac
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace driver
|
|
|
|
{
|
|
|
|
|
2015-07-27 15:04:54 -07:00
|
|
|
Kernel::Kernel(Program const & program, const char * name) : backend_(program.backend_), address_bits_(program.context().device().address_bits()), h_(backend_, true)
|
2015-04-29 15:50:57 -04:00
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
2015-08-25 12:41:21 -04:00
|
|
|
cu_params_store_.reserve(64);
|
|
|
|
cu_params_.reserve(64);
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuModuleGetFunction(&h_.cu(), program.h_.cu(), name);\
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
case OPENCL:
|
2015-07-25 21:00:18 -07:00
|
|
|
cl_int err;
|
2015-08-25 12:41:21 -04:00
|
|
|
h_.cl() = dispatch::clCreateKernel(program.h_.cl(), name, &err);
|
2016-04-02 18:19:33 -04:00
|
|
|
check(err);
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
void Kernel::setArg(unsigned int index, value_scalar const & scal)
|
|
|
|
{
|
|
|
|
switch(scal.dtype())
|
|
|
|
{
|
|
|
|
//case BOOL_TYPE: setArg(index, scal.values().bool8); break;
|
|
|
|
case CHAR_TYPE: setArg(index, scal.values().int8); break;
|
|
|
|
case UCHAR_TYPE: setArg(index, scal.values().uint8); break;
|
|
|
|
case SHORT_TYPE: setArg(index, scal.values().int16); break;
|
|
|
|
case USHORT_TYPE: setArg(index, scal.values().uint16); break;
|
|
|
|
case INT_TYPE: setArg(index, scal.values().int32); break;
|
|
|
|
case UINT_TYPE: setArg(index, scal.values().uint32); break;
|
|
|
|
case LONG_TYPE: setArg(index, scal.values().int64); break;
|
|
|
|
case ULONG_TYPE: setArg(index, scal.values().uint64); break;
|
|
|
|
//case HALF_TYPE: setArg(index, scal.values().float16); break;
|
|
|
|
case FLOAT_TYPE: setArg(index, scal.values().float32); break;
|
|
|
|
case DOUBLE_TYPE: setArg(index, scal.values().float64); break;
|
|
|
|
default: throw unknown_datatype(scal.dtype());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
void Kernel::setArg(unsigned int index, std::size_t size, void* ptr)
|
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
|
|
|
if(index + 1> cu_params_store_.size())
|
|
|
|
{
|
|
|
|
cu_params_store_.resize(index+1);
|
|
|
|
cu_params_.resize(index+1);
|
|
|
|
}
|
|
|
|
cu_params_store_[index].reset(malloc(size), free);
|
|
|
|
memcpy(cu_params_store_[index].get(), ptr, size);
|
|
|
|
cu_params_[index] = cu_params_store_[index].get();
|
|
|
|
break;
|
|
|
|
case OPENCL:
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::clSetKernelArg(h_.cl(), index, size, ptr);
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Kernel::setArg(unsigned int index, Buffer const & data)
|
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
|
|
|
{
|
2015-08-20 21:24:41 -04:00
|
|
|
setArg(index, sizeof(CUdeviceptr), (void*)&data.h_.cu()); break;
|
2015-04-29 15:50:57 -04:00
|
|
|
}
|
2015-07-25 21:00:18 -07:00
|
|
|
case OPENCL:
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::clSetKernelArg(h_.cl(), index, sizeof(cl_mem), (void*)&data.h_.cl());
|
2015-07-25 21:00:18 -07:00
|
|
|
break;
|
2015-04-29 15:50:57 -04:00
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Kernel::setSizeArg(unsigned int index, size_t N)
|
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
|
|
|
{
|
2015-08-13 15:44:58 -07:00
|
|
|
int NN = static_cast<cl_int>(N);
|
2015-04-29 15:50:57 -04:00
|
|
|
setArg(index, sizeof(int), &NN);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPENCL:
|
2015-08-11 11:50:49 -07:00
|
|
|
{
|
2015-08-13 15:44:58 -07:00
|
|
|
cl_int NN = static_cast<cl_int>(N);
|
2015-08-11 11:50:49 -07:00
|
|
|
setArg(index, 4, &NN);
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
2015-08-11 11:50:49 -07:00
|
|
|
}
|
2015-04-29 15:50:57 -04:00
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|