HPXCL
/home/diehl/git/hpxcl/cuda/device.hpp
00001 // Copyright (c)    2013 Damond Howard
00002 //                  2015 Patrick Diehl
00003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
00005 #pragma once
00006 #ifndef HPX_CUDA_DEVICE_HPP_
00007 #define HPX_CUDA_DEVICE_HPP_
00008 
00009 #include <hpx/hpx.hpp>
00010 
00011 #include "cuda/server/buffer.hpp"
00012 #include "cuda/server/program.hpp"
00013 #include "cuda/server/device.hpp"
00014 
00015 namespace hpx {
00016 namespace cuda {
00017 
00018 class device: public hpx::components::client_base<device, cuda::server::device> {
00019         typedef hpx::components::client_base<device, cuda::server::device> base_type;
00020 
00021 public:
00022         device() {
00023         }
00024 
00025         device(hpx::naming::id_type const& there, int dev) :
00026                         base_type(hpx::new_<cuda::server::device>(there, dev)) {
00027         }
00028 
00029         device(hpx::future<hpx::naming::id_type> && gid) :
00030                         base_type(std::move(gid)) {
00031         }
00032 
00036         void get_cuda_info() {
00037                 HPX_ASSERT(this->get_gid());
00038                 typedef server::device::get_cuda_info_action action_type;
00039                 hpx::apply<action_type>(this->get_gid());
00040         }
00041 
00047         void get_extended_cuda_info() {
00048                 HPX_ASSERT(this->get_gid());
00049                 typedef server::device::get_extended_cuda_info_action action_type;
00050                 hpx::apply<action_type>(this->get_gid());
00051         }
00052 
00057         hpx::lcos::future<int> get_device_architecture_major() {
00058                 HPX_ASSERT(this->get_gid());
00059                 typedef server::device::get_device_architecture_major_action action_type;
00060                 return hpx::async<action_type>(this->get_gid());
00061         }
00062 
00067         hpx::lcos::future<int> get_device_architecture_minor() {
00068                 HPX_ASSERT(this->get_gid());
00069                 typedef server::device::get_device_architecture_minor_action action_type;
00070                 return hpx::async<action_type>(this->get_gid());
00071         }
00072 
00078         static std::vector<int> get_all_devices(
00079                         std::vector<hpx::naming::id_type> localities) {
00080                 int num = 0;
00081                 std::vector<int> devices;
00082                 typedef server::device::get_all_devices_action action_type;
00083                 for (size_t i = 0; i < localities.size(); i++) {
00084                         num += hpx::async<action_type>(localities[i]).get();
00085                         for (int i = 0; i < num; i++) {
00086                                 devices.push_back(i);
00087                         }
00088                 }
00089                 return devices;
00090         }
00091 
00092         void set_device(int dev) {
00093                 HPX_ASSERT(this->get_gid());
00094                 typedef server::device::set_device_action action_type;
00095                 hpx::async<action_type>(this->get_gid(), dev);
00096         }
00097 
00098         hpx::lcos::future<int> get_device_id() {
00099                 HPX_ASSERT(this->get_gid());
00100                 typedef server::device::get_device_id_action action_type;
00101                 return hpx::async<action_type>(this->get_gid());
00102         }
00103 
00104         int get_device_id_sync() {
00105                 HPX_ASSERT(this->get_gid());
00106                 return get_device_id().get();
00107         }
00108 
00109         hpx::lcos::future<int> get_context() {
00110                 HPX_ASSERT(this->get_gid());
00111                 typedef server::device::get_context_action action_type;
00112                 return hpx::async<action_type>(this->get_gid());
00113         }
00114 
00115         int get_context_sync() {
00116 
00117                 return get_context().get();
00118         }
00119 
00120         hpx::lcos::future<int> wait() {
00121                 return server::device::wait();
00122         }
00123 
00127         hpx::cuda::program create_program_with_source_sync(std::string source) {
00128                 return create_program_with_source(source).get();
00129         }
00130 
00142         hpx::lcos::future<hpx::cuda::program> create_program_with_source(
00143                         std::string source) {
00144                 HPX_ASSERT(this->get_gid());
00145                 typedef server::device::create_program_with_source_action action_type;
00146                 return hpx::async<action_type>(this->get_gid(), source);
00147         }
00148 
00160         hpx::lcos::future<hpx::cuda::program> create_program_with_file(
00161                         std::string file) {
00162                 HPX_ASSERT(this->get_gid());
00163 
00164                 std::string source;
00165                 std::string tmp;
00166                 std::ifstream in(file);
00167 
00168                 if (!in) {
00169                         std::string errorMessage = "File ";
00170                         errorMessage += file;
00171                         errorMessage += " not found!";
00172                         HPX_THROW_EXCEPTION(hpx::no_success, "create_program_with_file",
00173                                         errorMessage);
00174                 }
00175 
00176                 while (in) {
00177                         getline(in, tmp);
00178                         source += tmp;
00179                         source += "\n";
00180                 }
00181 
00182                 typedef server::device::create_program_with_source_action action_type;
00183                 return hpx::async<action_type>(this->get_gid(), source);
00184         }
00185 
00196         hpx::lcos::future<hpx::cuda::buffer> create_buffer(size_t size) {
00197                 HPX_ASSERT(this->get_gid());
00198                 typedef server::device::create_buffer_action action_type;
00199                 return hpx::async<action_type>(this->get_gid(), size);
00200         }
00201 
00205         hpx::cuda::buffer create_buffer_sync(size_t size) {
00206                 HPX_ASSERT(this->get_gid());
00207                 return create_buffer(size).get();
00208         }
00209 };
00210 }
00211 }
00212 #endif //MANAGED_CUDA_COMPONENT_1_HPP
 All Classes Functions