PackerTest.cu 3.62 KB
Newer Older
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
#include <time.h>
#include <stdlib.h>
#include "psrdada_cpp/effelsberg/edd/Packer.cuh"

#include "gtest/gtest.h"

//TEST(PackerTest, check2bit)
//{
//    std::size_t n = 1024;
//    thrust::device_vector<float>  input(n);
//    thrust::device_vector<uint32_t>  output(n);
//
//    {
//      float minV = -2;
//      float maxV = 2;
//
//      srand (time(NULL));
//      for (int i =0; i < input.size(); i++)
//      {
//        input[i] = ((float(rand()) / RAND_MAX) - 0.5) * 2.5 * (maxV-minV) + maxV + minV;
//      }
//
//      thrust::fill(output.begin(), output.end(), 5);
//      cudaStream_t stream;
//      cudaStreamCreate(&stream);
//      psrdada_cpp::effelsberg::edd::pack<2>(input, output, minV, maxV, stream);
//      EXPECT_EQ(output.size(), n / 16);
//
//      float step = (maxV - minV) / 3;
//      float L2 = minV + step;
//      float L3 = minV + 2 * step;
//      float L4 = minV + 3 * step;
//
//      const size_t nbp = 16; // 16 samples per output value
//      for(int i = 0; i < input.size() / nbp; i++)
//      {
//          uint32_t of = output[i];
//          for (size_t j =0; j< nbp; j++)
//          {
//            uint32_t a = ((of >> (j *2)) & 3);
//            int k = i * nbp + j;
//            if (input[k] >= L4)
//              EXPECT_EQ(a, 3);
//            else if (input[k] >= L3)
//              EXPECT_EQ(a, 2);
//            else if (input[k] >= L2)
//              EXPECT_EQ(a, 1);
//            else
//              EXPECT_EQ(a, 0);
//          }
//      }
//    }
//}

class PackerTest: public ::testing::Test
{
  protected:
    thrust::device_vector<float>  input;
    thrust::device_vector<uint32_t>  output; 
    float minV;
    float maxV;
    cudaStream_t stream;

    void SetUp() override {
      input.resize(1024);
      minV = -2;
      maxV = 2;

      srand (time(NULL));
      for (int i =0; i < input.size(); i++)
      {
        input[i] = ((float(rand()) / RAND_MAX) - 0.5) * 2.5 * (maxV-minV) + maxV + minV;
      }

      cudaStreamCreate(&stream);
    }

    void TearDown()
    {
      cudaStreamDestroy(stream);
    }

    void checkOutputSize(unsigned int bit_depth)
    {
      //SCOPED_TRACE("Input Bitdepth: " << bit_depth );
      EXPECT_EQ(output.size(), input.size() / (32 / bit_depth));
    }


    void checkOutputValues(unsigned int bit_depth)
    {

      float step = (maxV - minV) /  ((1 << bit_depth) - 1);

      const size_t nbp = 32 / bit_depth; 
      for(int i = 0; i < input.size() / nbp; i++)
      {
          uint32_t of = output[i];
          for (size_t j =0; j< nbp; j++)
          {
            uint32_t a = ((of >> (j * bit_depth)) & ((1 << bit_depth) - 1));
            int k = i * nbp + j;

            if (input[k] <= minV)
              EXPECT_EQ(0, int (a)) << "input[ " << k << "] = " << input[k];
            else if (input[k] >= maxV)
              EXPECT_EQ(((1 << bit_depth) - 1), int (a)) << "input[ " << k << "] = " << input[k];
            else
              EXPECT_EQ(int((input[k] - minV) / step), int(a)) << "input[ " << k << "] = " << input[k];
          }
      }
    }
    };

TEST_F(PackerTest, 2bit)
{
  psrdada_cpp::effelsberg::edd::pack<2>(input, output, minV, maxV, stream);
  checkOutputSize(2);
  checkOutputValues(2);
}


TEST_F(PackerTest, 4bit)
{
  psrdada_cpp::effelsberg::edd::pack<4>(input, output, minV, maxV, stream);
  checkOutputSize(4);
  checkOutputValues(4);
}

TEST_F(PackerTest, 8bit)
{
  psrdada_cpp::effelsberg::edd::pack<8>(input, output, minV, maxV, stream);
  checkOutputSize(8);
  checkOutputValues(8);
}

TEST_F(PackerTest, 16bit)
{
  psrdada_cpp::effelsberg::edd::pack<16>(input, output, minV, maxV, stream);
  checkOutputSize(16);
  checkOutputValues(16);
}