summaryrefslogtreecommitdiff
path: root/volk/lib/qa_utils.cc
blob: 2dee61823512eac795d091bda834e3ee52a2470d (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "qa_utils.h"
#include <cstring>
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/tokenizer.hpp>
//#include <boost/test/unit_test.hpp>
#include <iostream>
#include <vector>
#include <list>
#include <ctime>
#include <cmath>
#include <boost/lexical_cast.hpp>
//#include <volk/volk_runtime.h>
#include <volk/volk_registry.h>
#include <volk/volk.h>
#include <volk/volk_cpu.h>
#include <volk/volk_common.h>
#include <boost/typeof/typeof.hpp>
#include <boost/type_traits.hpp>

float uniform() {
  return 2.0 * ((float) rand() / RAND_MAX - 0.5);	// uniformly (-1, 1)
}

template <class t>
void random_floats (t *buf, unsigned n)
{
  for (unsigned i = 0; i < n; i++)
    buf[i] = uniform ();
}

void load_random_data(void *data, volk_type_t type, unsigned int n) {
    if(type.is_complex) n *= 2;
    if(type.is_float) {
        if(type.size == 8) random_floats<double>((double *)data, n);
        else random_floats<float>((float *)data, n);
    } else {
        float int_max = float(uint64_t(2) << (type.size*8));
        if(type.is_signed) int_max /= 2.0;
        for(int i=0; i<n; i++) {
            float scaled_rand = (((float) (rand() - (RAND_MAX/2))) / static_cast<float>((RAND_MAX/2))) * int_max;
            //man i really don't know how to do this in a more clever way, you have to cast down at some point
            switch(type.size) {
            case 8:
                if(type.is_signed) ((int64_t *)data)[i] = (int64_t) scaled_rand;
                else ((uint64_t *)data)[i] = (uint64_t) scaled_rand;
            break;
            case 4:
                if(type.is_signed) ((int32_t *)data)[i] = (int32_t) scaled_rand;
                else ((uint32_t *)data)[i] = (uint32_t) scaled_rand;
            break;           
            case 2:
                if(type.is_signed) ((int16_t *)data)[i] = (int16_t) scaled_rand;
                else ((uint16_t *)data)[i] = (uint16_t) scaled_rand;
            break;
            case 1:
                if(type.is_signed) ((int8_t *)data)[i] = (int8_t) scaled_rand;
                else ((uint8_t *)data)[i] = (uint8_t) scaled_rand;
            break;
            default:
                throw "load_random_data: no support for data size > 8 or < 1"; //no shenanigans here
            }
        }
    }
}

static std::vector<std::string> get_arch_list(struct volk_func_desc desc) {
    std::vector<std::string> archlist;

    for(int i = 0; i < desc.n_archs; i++) {
        //if(!(archs[i+1] & volk_get_lvarch())) continue; //this arch isn't available on this pc
        archlist.push_back(std::string(desc.indices[i]));
    }
    
    return archlist;
}

volk_type_t volk_type_from_string(std::string name) {
    volk_type_t type;
    type.is_float = false;
    type.is_scalar = false;
    type.is_complex = false;
    type.is_signed = false;
    type.size = 0;
    type.str = name;
    
    if(name.size() < 2) throw std::string("name too short to be a datatype");
    
    //is it a scalar?
    if(name[0] == 's') { 
        type.is_scalar = true;
        name = name.substr(1, name.size()-1);
    }
    
    //get the data size
    int last_size_pos = name.find_last_of("0123456789");
    if(last_size_pos < 0) throw std::string("no size spec in type ").append(name);
    //will throw if malformed
    int size = boost::lexical_cast<int>(name.substr(0, last_size_pos+1));

    assert(((size % 8) == 0) && (size <= 64) && (size != 0));
    type.size = size/8; //in bytes
    
    for(int i=last_size_pos+1; i < name.size(); i++) {
        switch (name[i]) {
        case 'f':
            type.is_float = true;
            break;
        case 'i':
            type.is_signed = true;
            break;
        case 'c':
            type.is_complex = true;
            break;
        case 'u':
            type.is_signed = false;
            break;
        default:
            throw;
        }
    }
    
    return type;
}

static void get_signatures_from_name(std::vector<volk_type_t> &inputsig, 
                                   std::vector<volk_type_t> &outputsig, 
                                   std::string name) {
    boost::char_separator<char> sep("_");
    boost::tokenizer<boost::char_separator<char> > tok(name, sep);
    std::vector<std::string> toked;
    tok.assign(name);
    toked.assign(tok.begin(), tok.end());
    
    assert(toked[0] == "volk");
    toked.erase(toked.begin());

    //ok. we're assuming a string in the form
    //(sig)_(multiplier-opt)_..._(name)_(sig)_(multiplier-opt)_..._(alignment)

    enum { SIDE_INPUT, SIDE_NAME, SIDE_OUTPUT } side = SIDE_INPUT;
    std::string fn_name;
    volk_type_t type;
    BOOST_FOREACH(std::string token, toked) {
        try {
            type = volk_type_from_string(token);
            if(side == SIDE_NAME) side = SIDE_OUTPUT; //if this is the first one after the name...
            
            if(side == SIDE_INPUT) inputsig.push_back(type);
            else outputsig.push_back(type);
        } catch (...){
            if(token[0] == 'x') { //it's a multiplier
                if(side == SIDE_INPUT) assert(inputsig.size() > 0);
                else assert(outputsig.size() > 0);
                int multiplier = boost::lexical_cast<int>(token.substr(1, token.size()-1)); //will throw if invalid
                for(int i=1; i<multiplier; i++) {
                    if(side == SIDE_INPUT) inputsig.push_back(inputsig.back());
                    else outputsig.push_back(outputsig.back());
                }
            }
            else if(side == SIDE_INPUT) { //it's the function name, at least it better be
                side = SIDE_NAME;
                fn_name.append("_");
                fn_name.append(token);
            } 
            else if(side == SIDE_OUTPUT) {
                if(token != toked.back()) throw; //the last token in the name is the alignment
            }
        }
    }
    //we don't need an output signature (some fn's operate on the input data, "in place"), but we do need at least one input!
    assert(inputsig.size() != 0);
}

inline void run_cast_test1(volk_fn_1arg func, std::vector<void *> &buffs, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], vlen, arch.c_str());
}

inline void run_cast_test2(volk_fn_2arg func, std::vector<void *> &buffs, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], buffs[1], vlen, arch.c_str());
}

inline void run_cast_test3(volk_fn_3arg func, std::vector<void *> &buffs, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], buffs[1], buffs[2], vlen, arch.c_str());
}

inline void run_cast_test4(volk_fn_4arg func, std::vector<void *> &buffs, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], buffs[1], buffs[2], buffs[3], vlen, arch.c_str());
}

inline void run_cast_test1_s32f(volk_fn_1arg_s32f func, std::vector<void *> &buffs, float scalar, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], scalar, vlen, arch.c_str());
}

inline void run_cast_test2_s32f(volk_fn_2arg_s32f func, std::vector<void *> &buffs, float scalar, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], buffs[1], scalar, vlen, arch.c_str());
}

inline void run_cast_test3_s32f(volk_fn_3arg_s32f func, std::vector<void *> &buffs, float scalar, unsigned int vlen, unsigned int iter, std::string arch) {
    while(iter--) func(buffs[0], buffs[1], buffs[2], scalar, vlen, arch.c_str());
}

template <class t>
bool fcompare(t *in1, t *in2, unsigned int vlen, float tol) {
    bool fail = false;
    int print_max_errs = 10;
    for(int i=0; i<vlen; i++) {
        if(((t *)(in1))[i] < 1e-30) continue; //this is a hack: below around here we'll start to get roundoff errors due to limited precision
        if(fabs(((t *)(in1))[i] - ((t *)(in2))[i])/(((t *)in1)[i]) > tol) {
            fail=true;
            if(print_max_errs-- > 0) {
                std::cout << "offset " << i << " in1: " << t(((t *)(in1))[i]) << " in2: " << t(((t *)(in2))[i]) << std::endl;
            }
        }
    }
    
    return fail;
}

template <class t>
bool icompare(t *in1, t *in2, unsigned int vlen, unsigned int tol) {
    bool fail = false;
    int print_max_errs = 10;
    for(int i=0; i<vlen; i++) {
        if(abs(((t *)(in1))[i] - ((t *)(in2))[i]) > tol) {
            fail=true;
            if(print_max_errs-- > 0) {
                std::cout << "offset " << i << " in1: " << static_cast<int>(t(((t *)(in1))[i])) << " in2: " << static_cast<int>(t(((t *)(in2))[i])) << std::endl;
            }
        }
    }
    
    return fail;
}

class volk_qa_aligned_mem_pool{
public:
    void *get_new(size_t size, size_t alignment = 16){
        _mems.push_back(std::vector<char>(size + alignment-1, 0));
        size_t ptr = size_t(&_mems.back().front());
        return (void *)((ptr + alignment-1) & ~(alignment-1));
    }
private: std::list<std::vector<char> > _mems;
};

bool run_volk_tests(struct volk_func_desc desc, void (*manual_func)(), std::string name, float tol, float scalar, int vlen, int iter) {
    std::cout << "RUN_VOLK_TESTS: " << name << std::endl;
    
    //first let's get a list of available architectures for the test
    std::vector<std::string> arch_list = get_arch_list(desc);
    
    if(arch_list.size() < 2) {
        std::cout << "no architectures to test" << std::endl;
        return false;
    }

    //something that can hang onto memory and cleanup when this function exits
    volk_qa_aligned_mem_pool mem_pool;

    //now we have to get a function signature by parsing the name
    std::vector<volk_type_t> inputsig, outputsig;
    get_signatures_from_name(inputsig, outputsig, name);
    
    //pull the input scalars into their own vector
    std::vector<volk_type_t> inputsc;
    for(int i=0; i<inputsig.size(); i++) {
        if(inputsig[i].is_scalar) {
            inputsc.push_back(inputsig[i]);
            inputsig.erase(inputsig.begin() + i);
        }
    }

    //for(int i=0; i<inputsig.size(); i++) std::cout << "Input: " << inputsig[i].str << std::endl;
    //for(int i=0; i<outputsig.size(); i++) std::cout << "Output: " << outputsig[i].str << std::endl;
    std::vector<void *> inbuffs;
    BOOST_FOREACH(volk_type_t sig, inputsig) {
        if(!sig.is_scalar) //we don't make buffers for scalars
          inbuffs.push_back(mem_pool.get_new(vlen*sig.size*(sig.is_complex ? 2 : 1)));
    }
    for(int i=0; i<inbuffs.size(); i++) {
        load_random_data(inbuffs[i], inputsig[i], vlen);
    }
    
    //ok let's make a vector of vector of void buffers, which holds the input/output vectors for each arch
    std::vector<std::vector<void *> > test_data;
    for(int i=0; i<arch_list.size(); i++) {
        std::vector<void *> arch_buffs;
        for(int j=0; j<outputsig.size(); j++) {
            arch_buffs.push_back(mem_pool.get_new(vlen*outputsig[j].size*(outputsig[j].is_complex ? 2 : 1)));
        }
        for(int j=0; j<inputsig.size(); j++) {
            arch_buffs.push_back(inbuffs[j]);
        }
        test_data.push_back(arch_buffs);
    }
    
    std::vector<volk_type_t> both_sigs;
    both_sigs.insert(both_sigs.end(), outputsig.begin(), outputsig.end());
    both_sigs.insert(both_sigs.end(), inputsig.begin(), inputsig.end());

    //now run the test
    clock_t start, end;
    for(int i = 0; i < arch_list.size(); i++) {
        start = clock();

        switch(both_sigs.size()) {
            case 1:
                if(inputsc.size() == 0) {
                    run_cast_test1((volk_fn_1arg)(manual_func), test_data[i], vlen, iter, arch_list[i]); 
                } else if(inputsc.size() == 1 && inputsc[0].is_float) {
                    run_cast_test1_s32f((volk_fn_1arg_s32f)(manual_func), test_data[i], scalar, vlen, iter, arch_list[i]);
                } else throw "unsupported 1 arg function >1 scalars";
                break;
            case 2:
                if(inputsc.size() == 0) {
                    run_cast_test2((volk_fn_2arg)(manual_func), test_data[i], vlen, iter, arch_list[i]);
                } else if(inputsc.size() == 1 && inputsc[0].is_float) {
                    run_cast_test2_s32f((volk_fn_2arg_s32f)(manual_func), test_data[i], scalar, vlen, iter, arch_list[i]);
                } else throw "unsupported 2 arg function >1 scalars";
                break;
            case 3:
                if(inputsc.size() == 0) {
                    run_cast_test3((volk_fn_3arg)(manual_func), test_data[i], vlen, iter, arch_list[i]);
                } else if(inputsc.size() == 1 && inputsc[0].is_float) {
                    run_cast_test3_s32f((volk_fn_3arg_s32f)(manual_func), test_data[i], scalar, vlen, iter, arch_list[i]);
                } else throw "unsupported 3 arg function >1 scalars";
                break;
            case 4:
                run_cast_test4((volk_fn_4arg)(manual_func), test_data[i], vlen, iter, arch_list[i]);
                break;
            default:
                throw "no function handler for this signature";
                break;
        }
        
        end = clock();
        std::cout << arch_list[i] << " completed in " << (double)(end-start)/(double)CLOCKS_PER_SEC << "s" << std::endl;
    }
    //and now compare each output to the generic output
    //first we have to know which output is the generic one, they aren't in order...
    int generic_offset=0;
    for(int i=0; i<arch_list.size(); i++) 
        if(arch_list[i] == "generic") generic_offset=i;

    //now compare
    //if(outputsig.size() == 0) outputsig = inputsig; //a hack, i know
    
    bool fail = false;
    bool fail_global = false;
    for(int i=0; i<arch_list.size(); i++) {
        if(i != generic_offset) {
            for(int j=0; j<both_sigs.size(); j++) {
                if(both_sigs[j].is_float) {
                    if(both_sigs[j].size == 8) {
                        fail = fcompare((double *) test_data[generic_offset][j], (double *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                    } else {
                        fail = fcompare((float *) test_data[generic_offset][j], (float *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                    }
                } else {
                    //i could replace this whole switch statement with a memcmp if i wasn't interested in printing the outputs where they differ
                    switch(both_sigs[j].size) {
                    case 8:
                        if(both_sigs[j].is_signed) {
                            fail = icompare((int64_t *) test_data[generic_offset][j], (int64_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        } else {
                            fail = icompare((uint64_t *) test_data[generic_offset][j], (uint64_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        }
                        break;
                    case 4:
                        if(both_sigs[j].is_signed) {
                            fail = icompare((int32_t *) test_data[generic_offset][j], (int32_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        } else {
                            fail = icompare((uint32_t *) test_data[generic_offset][j], (uint32_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        }
                        break;
                    case 2:
                        if(both_sigs[j].is_signed) {
                            fail = icompare((int16_t *) test_data[generic_offset][j], (int16_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        } else {
                            fail = icompare((uint16_t *) test_data[generic_offset][j], (uint16_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        }
                        break;
                    case 1:
                        if(both_sigs[j].is_signed) {
                            fail = icompare((int8_t *) test_data[generic_offset][j], (int8_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        } else {
                            fail = icompare((uint8_t *) test_data[generic_offset][j], (uint8_t *) test_data[i][j], vlen*(both_sigs[j].is_complex ? 2 : 1), tol);
                        }
                        break;
                    default:
                        fail=1;
                    }
                }
                if(fail) {
                    fail_global = true;
                    std::cout << name << ": fail on arch " << arch_list[i] << std::endl;
                }
                //fail = memcmp(outbuffs[generic_offset], outbuffs[i], outputsig[0].size * vlen * (outputsig[0].is_complex ? 2:1));
            }
        }
    }

    return fail_global;
}