summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/client/clfunc_xgemv.hpp
blob: cc851094569af91de0b4fa834ee14170fee99ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* ************************************************************************
 * Copyright 2013 Advanced Micro Devices, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ************************************************************************/


// $Id

#ifndef CLBLAS_BENCHMARK_XGEMV_HXX__
#define CLBLAS_BENCHMARK_XGEMV_HXX__

#include "clfunc_common.hpp"

template <typename T>
struct xGemvBuffer
{
    clblasOrder order_;
    size_t m_;
    size_t n_;
    size_t lda_;
    size_t offA_;
    size_t a_num_vectors_;
    size_t b_num_vectors_;
    size_t c_num_vectors_;
    clblasTranspose trans_a_;
    T* a_;
    T* x_;
    T* y_;
    cl_mem buf_a_;
    cl_mem buf_x_;
    cl_mem buf_y_;
    T alpha_;
    T beta_;
}; // struct buffer

template <typename T>
class xGemv : public clblasFunc
{
public:
    xGemv(StatisticalTimer& timer, cl_device_type devType) :
        clblasFunc(timer, devType)
    {
        timer.getUniqueID("clGemv", 0);
    }

    ~xGemv()
    {
        delete buffer_.a_;
        delete buffer_.x_;
        delete buffer_.y_;
        OPENCL_V_THROW( clReleaseMemObject(buffer_.buf_a_),
                        "releasing buffer A");
        OPENCL_V_THROW( clReleaseMemObject(buffer_.buf_x_),
                        "releasing buffer X");
        OPENCL_V_THROW( clReleaseMemObject(buffer_.buf_y_),
                        "releasing buffer Y");
    }

    void call_func()
    {
    }

    double gflops()
    {
        return (2.0*buffer_.m_*buffer_.n_)/time_in_ns();
    }

    std::string gflops_formula()
    {
        return "2.0*M*N/time";
        // NOTE i removed a \n from the end of this. it needs to be absent
        // from all functions
    }

    void setup_buffer(int order_option, int side_option, int uplo_option,
                      int diag_option, int transA_option, int  transB_option,
                      size_t M, size_t N, size_t K, size_t lda, size_t ldb,
                      size_t ldc, size_t offA, size_t offBX, size_t offCY,
                      double alpha, double beta)
    {
        DUMMY_ARGS_USAGE_4(side_option, uplo_option, diag_option,
                           transB_option);
        DUMMY_ARGS_USAGE_3(K, ldb, ldc);
        DUMMY_ARGS_USAGE_2(offBX, offCY);

        initialize_scalars(alpha, beta);

        buffer_.m_ = M;
        buffer_.n_ = N;
        buffer_.offA_ = offA;

        if (transA_option == 0)
        {
            buffer_.trans_a_ = clblasNoTrans;
            buffer_.x_ = new T[buffer_.n_];
            buffer_.y_ = new T[buffer_.m_];
        }
        else
        {
            buffer_.trans_a_ = clblasTrans;
            buffer_.x_ = new T[buffer_.m_];
            buffer_.y_ = new T[buffer_.n_];
        }

        if (order_option == 0)
        {
            order_ = clblasRowMajor;
            buffer_.a_num_vectors_ = M;
            if (lda == 0)
            {
                buffer_.lda_ = N;
            }
            else if (lda < N)
            {
                std::cerr << "lda:wrong size\n";
                exit(1);
            }
            else
            {
                buffer_.lda_ = lda;
            }
        }
        else
        {
            order_ = clblasColumnMajor;
            buffer_.a_num_vectors_ = N;
            if (lda == 0)
            {
                buffer_.lda_ = M;
            }
            else if (lda < M)
            {
                std::cerr << "lda:wrong size\n";
                exit(1);
            }
            else
            {
                buffer_.lda_ = lda;
            }

        }
        buffer_.a_ = new T[buffer_.lda_*buffer_.a_num_vectors_];

        cl_int err;
        size_t size = (buffer_.lda_ * buffer_.a_num_vectors_ + buffer_.offA_) * sizeof(T);
        if( size >= maxMemAllocSize )
            throw std::runtime_error( "Tried to create a buffer larger than allowable on this device" );

        buffer_.buf_a_ = clCreateBuffer(ctx_, CL_MEM_READ_ONLY,
                                        size,
                                        NULL, &err);

        if (transA_option == 0)
        {
            buffer_.buf_x_ = clCreateBuffer(ctx_, CL_MEM_READ_ONLY,
                                            buffer_.n_*sizeof(T),
                                            NULL, &err);

            buffer_.buf_y_ = clCreateBuffer(ctx_, CL_MEM_READ_WRITE,
                                            buffer_.m_*sizeof(T),
                                            NULL, &err);
        }
        else
        {
            buffer_.buf_x_ = clCreateBuffer(ctx_, CL_MEM_READ_ONLY,
                                            buffer_.m_*sizeof(T),
                                            NULL, &err);

            buffer_.buf_y_ = clCreateBuffer(ctx_, CL_MEM_READ_WRITE,
                                            buffer_.n_*sizeof(T),
                                            NULL, &err);
        }
    }

    void initialize_cpu_buffer()
    {
        srand(10);
        for (size_t i = 0; i < buffer_.a_num_vectors_; ++i)
        {
            for (size_t j = 0; j < buffer_.lda_; ++j)
            {
                buffer_.a_[i*buffer_.lda_+j] = random<T>(UPPER_BOUND<T>()) /
                                               randomScale<T>();
            }
        }

        if (buffer_.trans_a_ == clblasNoTrans)
        {
            for (size_t i = 0; i < buffer_.n_; ++i)
            {
                buffer_.x_[i] = random<T>(UPPER_BOUND<T>()) /
                                randomScale<T>();
            }
            for (size_t i = 0; i < buffer_.m_; ++i)
            {
                buffer_.y_[i] = random<T>(UPPER_BOUND<T>()) /
                                randomScale<T>();
            }
        }
        else
        {
            for (size_t i = 0; i < buffer_.m_; ++i)
            {
                buffer_.x_[i] = random<T>(UPPER_BOUND<T>()) /
                                randomScale<T>();
            }
            for (size_t i = 0; i < buffer_.n_; ++i)
            {
                buffer_.y_[i] = random<T>(UPPER_BOUND<T>()) /
                                randomScale<T>();
            }
        }
    }

    void initialize_gpu_buffer()
    {
        cl_int err;

        err = clEnqueueWriteBuffer(queue_, buffer_.buf_a_, CL_TRUE,
                                   buffer_.offA_ * sizeof(T),
                                   buffer_.lda_ * buffer_.a_num_vectors_ *
                                       sizeof(T),
                                   buffer_.a_, 0, NULL, NULL);

        if (buffer_.trans_a_ == clblasNoTrans)
        {
            err = clEnqueueWriteBuffer(queue_, buffer_.buf_x_, CL_TRUE, 0,
                                       buffer_.n_*sizeof(T),
                                 buffer_.x_, 0, NULL, NULL);

            err = clEnqueueWriteBuffer(queue_, buffer_.buf_y_, CL_TRUE, 0,
                                       buffer_.m_*sizeof(T),
                                       buffer_.y_, 0, NULL, NULL);
        }
        else
        {
            err = clEnqueueWriteBuffer(queue_, buffer_.buf_x_, CL_TRUE, 0,
                                       buffer_.m_*sizeof(T),
                                       buffer_.x_, 0, NULL, NULL);

            err = clEnqueueWriteBuffer(queue_, buffer_.buf_y_, CL_TRUE, 0,
                                       buffer_.n_*sizeof(T),
                                       buffer_.y_, 0, NULL, NULL);
        }
    }

    void reset_gpu_write_buffer()
    {
        cl_int err;

        if (buffer_.trans_a_ == clblasNoTrans)
        {
            err = clEnqueueWriteBuffer(queue_, buffer_.buf_y_, CL_TRUE, 0,
                                       buffer_.m_*sizeof(T),
                                       buffer_.y_, 0, NULL, NULL);
        }
        else
        {
            err = clEnqueueWriteBuffer(queue_, buffer_.buf_y_, CL_TRUE, 0,
                                       buffer_.n_*sizeof(T),
                                       buffer_.y_, 0, NULL, NULL);
        }
    }

	void read_gpu_buffer()
	{
		//to-do need to fill up
	}
	void roundtrip_func()
	{//to-do need to fill up
	}
	void roundtrip_setup_buffer(int order_option, int side_option, int uplo_option,
                      int diag_option, int transA_option, int  transB_option,
                      size_t M, size_t N, size_t K, size_t lda, size_t ldb,
                      size_t ldc, size_t offA, size_t offBX, size_t offCY,
                      double alpha, double beta)
		{}
	void releaseGPUBuffer_deleteCPUBuffer()
	{
		//this is necessary since we are running a iteration of tests and calculate the average time. (in client.cpp)
		//need to do this before we eventually hit the destructor
		//to-do
	}

protected:
    void initialize_scalars(double alpha, double beta)
    {
        buffer_.alpha_ = makeScalar<T>(alpha);
        buffer_.beta_ = makeScalar<T>(beta);
    }

private:
  xGemvBuffer<T> buffer_;

}; // class xgemv


template<>
void
xGemv<float>::
call_func()
{
    timer.Start(timer_id);

	clblasSgemv(order_, buffer_.trans_a_, buffer_.m_, buffer_.n_,
                     buffer_.alpha_, buffer_.buf_a_, buffer_.offA_,
                     buffer_.lda_, buffer_.buf_x_, 0, 1, buffer_.beta_,
                     buffer_.buf_y_, 0, 1, 1, &queue_, 0, NULL, &event_);

    clWaitForEvents(1, &event_);
    timer.Stop(timer_id);
}

template<>
void
xGemv<double>::
call_func()
{
    timer.Start(timer_id);

	clblasDgemv(order_, buffer_.trans_a_, buffer_.m_, buffer_.n_,
                     buffer_.alpha_, buffer_.buf_a_, buffer_.offA_,
                     buffer_.lda_, buffer_.buf_x_, 0, 1, buffer_.beta_,
                     buffer_.buf_y_, 0, 1, 1, &queue_, 0, NULL, &event_);

    clWaitForEvents(1, &event_);
    timer.Stop(timer_id);
}

template<>
void
xGemv<cl_float2>::
call_func()
{
    timer.Start(timer_id);

	clblasCgemv(order_, buffer_.trans_a_, buffer_.m_, buffer_.n_,
                     buffer_.alpha_, buffer_.buf_a_, buffer_.offA_,
                     buffer_.lda_, buffer_.buf_x_, 0, 1, buffer_.beta_,
                     buffer_.buf_y_, 0, 1, 1, &queue_, 0, NULL, &event_);

	clWaitForEvents(1, &event_);
    timer.Stop(timer_id);
}

template<>
void
xGemv<cl_double2>::
call_func()
{
    timer.Start(timer_id);

	clblasZgemv(order_, buffer_.trans_a_, buffer_.m_, buffer_.n_,
                     buffer_.alpha_, buffer_.buf_a_, buffer_.offA_,
                     buffer_.lda_, buffer_.buf_x_, 0, 1, buffer_.beta_,
                     buffer_.buf_y_, 0, 1, 1, &queue_, 0, NULL, &event_);

    clWaitForEvents(1, &event_);
    timer.Stop(timer_id);
}

#endif // ifndef CLBLAS_BENCHMARK_XGEMV_HXX__