Skip to content

Commit

Permalink
Merge pull request #25 from Polytonic/development
Browse files Browse the repository at this point in the history
Add Windows Support
  • Loading branch information
Polytonic committed Sep 5, 2015
2 parents 33d8893 + 82dcf8d commit 85d6686
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 11 deletions.
21 changes: 18 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@ before_script:

# Run the Test Script
script:
- mkdir -p build
- cd build
- cmake ..
- make all
- cmake --build .

# Test the Core Binaries
- ./clinfo > /dev/null
- ./chlorine > /dev/null

# Test the Mandelbrot Example
- make mandelbrot
- cd examples/mandelbrot
- ./mandelbrot > /dev/null
- cd -

# Test the Swap Example
- make swap
- cd examples/swap
- ./swap > /dev/null
- cd -

# Run the Test Suite
- make check

# Upload Code Coverage Data
after_success:
- coveralls --root .. --exclude chlorine/include/cl.hpp --exclude chlorine/clinfo/clinfo.cpp --exclude examples --exclude tests
- coveralls --root .. --exclude chlorine/include/cl.hpp --exclude chlorine/clinfo/clinfo.cpp --exclude tests

# Display Compiler Information
after_script:
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ add_custom_command(TARGET tests PRE_BUILD
# Workaround for Reserved CMAKE Keyword: test
add_custom_target(check COMMAND tests --reporter compact DEPENDS tests)

# Build the Examples
add_subdirectory(examples/swap EXCLUDE_FROM_ALL)
add_subdirectory(examples/mandelbrot EXCLUDE_FROM_ALL)

# Build the Documentation
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/.doxyfile
Expand Down
5 changes: 5 additions & 0 deletions build/make_check.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake --build . --target tests
pushd Debug
tests.exe
popd
pause
85 changes: 84 additions & 1 deletion chlorine/include/chlorine.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
/**
The MIT License (MIT)
Copyright (c) 2015 Kevin Fung
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

// Chlorine Header
#ifndef CHLORINE
#define CHLORINE
#pragma once

// Include OpenCL C++ Bindings
#pragma GCC diagnostic push
Expand All @@ -14,6 +39,7 @@
#include <iostream>
#include <map>
#include <string>
#include <valarray>
#include <vector>

namespace ch
Expand Down Expand Up @@ -74,6 +100,16 @@ namespace ch
template<unsigned int const argn = 0, template<typename ...> class V, typename T, typename ... Params>
cl::Event call(std::string const & kernel_function, V<T> & array, Params && ... parameters);

#ifdef _WIN32
// Explicitly Provide Prototype for STL Valarrays
template<unsigned int const argn = 0, class T, typename ... Params>
cl::Event call(std::string const & kernel_function, std::valarray<T> & array, Params && ... parameters);

// Explicitly Provide Prototype for STL Vectors
template<unsigned int const argn = 0, class T, typename ... Params>
cl::Event call(std::string const & kernel_function, std::vector<T> & array, Params && ... parameters);
#endif

private:

// Components
Expand Down Expand Up @@ -336,6 +372,53 @@ namespace ch
mKernels[kernel_function].setArg(argn, buffer);
return call<argn+1>(kernel_function, parameters...);
}
};

#ifdef _WIN32
/**
Sets the kernel argument at the current index to an OpenCL buffer
mapped to the contents of a std::valarray.
@attention this method is only available under MSVC.
@note this is an overloaded, recursive, variadic template method.
@param kernel_function the name of the kernel function to call.
@param array a std::valarray containing your data.
@param parameters arguments to forward to other variadic overloads.
@returns an OpenCL event object containing profiling data.
*/
template<unsigned int const argn = 0, class T, typename ... Params>
cl::Event Worker::call(std::string const & kernel_function, std::valarray<T> & array, Params && ... parameters)
{
size_t array_size = array.size() * sizeof(T);
if (array.size() > mGlobal[0]) { mGlobal = cl::NDRange(array.size()); }
cl::Buffer buffer = cl::Buffer(mContext, CL_MEM_USE_HOST_PTR, array_size, & array[0]);
mBuffers.push_back(std::make_pair(buffer, array_size));
mKernels[kernel_function].setArg(argn, buffer);
return call<argn+1>(kernel_function, parameters...);
}

/**
Sets the kernel argument at the current index to an OpenCL buffer
mapped to the contents of a std::vector.
@attention this method is only available under MSVC.
@note this is an overloaded, recursive, variadic template method.
@param kernel_function the name of the kernel function to call.
@param array a std::vector containing your data.
@param parameters arguments to forward to other variadic overloads.
@returns an OpenCL event object containing profiling data.
*/
template<unsigned int const argn = 0, class T, typename ... Params>
cl::Event Worker::call(std::string const & kernel_function, std::vector<T> & array, Params && ... parameters)
{
size_t array_size = array.size() * sizeof(T);
if (array.size() > mGlobal[0]) { mGlobal = cl::NDRange(array.size()); }
cl::Buffer buffer = cl::Buffer(mContext, CL_MEM_USE_HOST_PTR, array_size, & array[0]);
mBuffers.push_back(std::make_pair(buffer, array_size));
mKernels[kernel_function].setArg(argn, buffer);
return call<argn+1>(kernel_function, parameters...);
}
#endif

}; //~ Chlorine Namespace

#endif
41 changes: 37 additions & 4 deletions chlorine/src/chlorine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
// Include the Chlorine Header
#include "chlorine.hpp"
int main()

// Visual Studio has trouble finding files on disk because the working directory
// is not set using CMake. For the purposes of having a "clone and play" sample,
// that runs on every platform, I have hard-coded the sample kernel here.
static const char* kernel =
"__kernel void swap(__global float * spam, __global float * eggs)"
"{"
" unsigned int i = get_global_id(0);"
" float swap = spam[i];"
" spam[i] = eggs[i];"
" eggs[i] = swap;"
"}"
;

// Slightly Modified Sample Code from Readme
int main(int argc, char * argv[])
{
std::cout << "Hello World\n";
return 0;
}
// Create Some Data
std::vector<float> spam(10, 3.1415f);
std::vector<float> eggs(10, 2.7182f);

// Initialize a Chlorine Worker
ch::Worker worker;

// Build the OpenCL Kernel
worker.build_kernel(kernel);

// Call the Swap Function in the Given Kernel
auto event = worker.call("swap", spam, eggs);

// Host Containers Are Automatically Updated
std::cout << "Spam: " << spam[0] << "\n"; // 2.7182
std::cout << "Eggs: " << eggs[0] << "\n"; // 3.1415

// Print Some Profiling Data
std::cout << "Elapsed Time: " << ch::elapsed(event) << "ns\n";
}
2 changes: 1 addition & 1 deletion examples/mandelbrot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ add_executable(mandelbrot ${PROJECT_HEADERS} ${PROJECT_SOURCES}
target_link_libraries(mandelbrot ${OpenCL_LIBRARY})
add_custom_command(TARGET mandelbrot PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/mandelbrot.cl
${PROJECT_SOURCE_DIR}/mandelbrot.cl
$<TARGET_FILE_DIR:mandelbrot>/mandelbrot.cl)
2 changes: 1 addition & 1 deletion examples/swap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ add_executable(swap ${PROJECT_HEADERS} ${PROJECT_SOURCES}
target_link_libraries(swap ${OpenCL_LIBRARY})
add_custom_command(TARGET swap PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/swap.cl
${PROJECT_SOURCE_DIR}/swap.cl
$<TARGET_FILE_DIR:swap>/swap.cl)
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ documentation blocks are available above method definitions. The class declarati
## License
>The MIT License (MIT)
>Copyright (c) 2014 Kevin Fung
>Copyright (c) 2015 Kevin Fung
>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Expand Down

0 comments on commit 85d6686

Please sign in to comment.