clruntime.cpp 7.26 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
5
6
7
// ================================================================================================
// 
// This file is part of the KissCL, an OpenCL C++ wrapper following the KISS principle.
// 
// Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge (software@cszb.net)
// 
8
9
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
schultezub's avatar
schultezub committed
10
// 
11
12
13
14
15
//  * Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright notice, this list of
//    conditions and the following disclaimer in the documentation and/or other materials provided
//    with the distribution.
schultezub's avatar
schultezub committed
16
17
//  * The names of its contributors may not be used to endorse or promote products derived from
//    this software without specific prior written permission.
18
19
20
21
22
23
24
25
26
27
//  
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
//  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
//  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
//  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
schultezub's avatar
schultezub committed
28
29
30
// 
// ================================================================================================

31
32
33
34
#include "clruntime.h"

#include "tgt/logmanager.h"

schultezub's avatar
schultezub committed
35
namespace kisscl {
36

schultezub's avatar
schultezub committed
37
    const std::string CLRuntime::loggerCat_ = "kisscl.CLRuntime";
38

schultezub's avatar
schultezub committed
39
40
    CLRuntime::CLRuntime()
        : tgt::ResourceManager<Program>(false)
schultezub's avatar
KissCL:    
schultezub committed
41
        , _commandQueueProperties(0)
schultezub's avatar
schultezub committed
42
    {
43
44
45
46
        initPlatforms();
    }

    CLRuntime::~CLRuntime() {
schultezub's avatar
schultezub committed
47
48
49
50
51
        // delete all command queues
         for (std::map< std::pair<Context*, Device*>, CommandQueue*>::iterator it = _commandQueues.begin(); it != _commandQueues.end(); ++it)
             delete it->second;

        // delete all platforms and with it all devices
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        for (std::vector<Platform*>::iterator it = _platforms.begin(); it != _platforms.end(); ++it)
            delete *it;
    }

    void CLRuntime::initPlatforms() {
        // gather OpenCL platforms and create wrapper objects
        cl_uint numPlatforms;
        LCL_ERROR(clGetPlatformIDs(0, 0, &numPlatforms));
        LINFO("Number of platformIds: " << numPlatforms);

        cl_platform_id* platformIds = new cl_platform_id[numPlatforms];
        LCL_ERROR(clGetPlatformIDs(numPlatforms, platformIds, 0));

        for(cl_uint i=0; i<numPlatforms; ++i) {
            _platforms.push_back(new Platform(platformIds[i]));
        }
        delete[] platformIds;

        // sort devices into shortcut lists
        for (std::vector<Platform*>::const_iterator pit = _platforms.begin(); pit != _platforms.end(); ++pit) {
            for (std::vector<Device*>::const_iterator dit = (*pit)->getDevices().begin(); dit != (*pit)->getDevices().end(); ++dit) {
                // check for CPU device type
                if ((*dit)->getDeviceType() & CL_DEVICE_TYPE_CPU)
                    _cpuDevices.push_back(*dit);

                // check for GPU device type
                if ((*dit)->getDeviceType() & CL_DEVICE_TYPE_GPU)
                    _gpuDevices.push_back(*dit);
            }
        }
    }

schultezub's avatar
KissCL:    
schultezub committed
84
    const std::vector<Device*>& CLRuntime::getCPUDevices() const {
85
86
87
        return _cpuDevices;
    }

schultezub's avatar
KissCL:    
schultezub committed
88
    const std::vector<Device*>& CLRuntime::getGPUDevices() const {
89
90
91
92
93
94
95
96
        return _gpuDevices;
    }

    Context* CLRuntime::createGlSharingContext(const std::vector<ContextProperty>& additionalProperties /*= std::vector<ContextProperty>()*/) const {
        Context* toReturn = 0;
        std::vector<ContextProperty> properties = Context::generateGlSharingProperties();
        properties.insert(properties.end(), additionalProperties.begin(), additionalProperties.end());

97
98
99
100
101
102
103
104
105
106
        for (std::vector<Device*>::const_iterator it = _gpuDevices.begin(); it != _gpuDevices.end(); ++it) {
            toReturn = new Context(*it, properties);
            if (toReturn->isValid())
                return toReturn;

            // else 
            delete toReturn;
            toReturn = 0;
        }

schultezub's avatar
schultezub committed
107
        for (std::vector<Device*>::const_iterator it = _cpuDevices.begin(); it != _cpuDevices.end(); ++it) {
108
109
110
111
112
113
114
115
116
117
118
119
            toReturn = new Context(*it, properties);
            if (toReturn->isValid())
                return toReturn;

            // else 
            delete toReturn;
            toReturn = 0;
        }

        return 0;
    }

schultezub's avatar
schultezub committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    Program* CLRuntime::loadProgram(Context* context, const std::string& filename) {
        std::vector<std::string> v;
        v.push_back(filename);
        return loadProgram(context, v);
    }

    Program* CLRuntime::loadProgram(Context* context, const std::vector<std::string>& filenames) {
        tgtAssert(context != 0, "Context must not be 0.");

        // complete paths and build unique identifier for resource manager
        std::string concatenatedFilenamens;
        std::vector<std::string> completedFilenmaes;
        completedFilenmaes.reserve(filenames.size());
        for (std::vector<std::string>::const_iterator it = filenames.begin(); it != filenames.end(); ++it) {
            completedFilenmaes.push_back(completePath(*it));
            concatenatedFilenamens.append(*it + "#");
        }

        Program* toReturn = new Program(context);
        toReturn->setHeader(_globalHeader);
        toReturn->loadFromFiles(completedFilenmaes);

        reg(toReturn, concatenatedFilenamens);

        return toReturn;
    }

    const std::string& CLRuntime::getGlobalHeader() const {
        return _globalHeader;
    }

    void CLRuntime::setGlobalHeader(const std::string& header) {
        _globalHeader = header;
    }


schultezub's avatar
KissCL:    
schultezub committed
156
157
158
159
160
161
    CommandQueue* CLRuntime::getCommandQueue(Context* context, Device* device /*= 0*/) {
        tgtAssert(context != 0, "Context may not be 0.");

        if (device == 0)
            device = context->getDevices().front();

schultezub's avatar
schultezub committed
162
163
        std::pair<Context*, Device*> p = std::make_pair(context, device);

164
        std::map< std::pair<Context*, Device*>, CommandQueue*>::iterator lb = _commandQueues.lower_bound(p);
schultezub's avatar
schultezub committed
165
        if (lb == _commandQueues.end() || lb ->first != p) {
schultezub's avatar
KissCL:    
schultezub committed
166
            CommandQueue* queue = new CommandQueue(context, device, _commandQueueProperties);
schultezub's avatar
schultezub committed
167
168
169
170
171
172
173
174
            _commandQueues.insert(lb, std::make_pair(p, queue));
            return queue;
        }
        else {
            return lb->second;
        }
    }

schultezub's avatar
KissCL:    
schultezub committed
175
176
177
178
179
180
181
182
    cl_command_queue_properties CLRuntime::getCommandQueueProperties() const {
        return _commandQueueProperties;
    }

    void CLRuntime::setCommandQueueProperties(cl_command_queue_properties cqp) {
        _commandQueueProperties = cqp;
    }

183
}