CoreNEURON
alignment.cpp
Go to the documentation of this file.
1 /*
2 # =============================================================================
3 # Copyright (c) 2016 - 2022 Blue Brain Project/EPFL
4 #
5 # See top-level LICENSE file for details.
6 # =============================================================================.
7 */
9 
10 #include <boost/mpl/list.hpp>
11 #define BOOST_TEST_MODULE PaddingCheck
12 #include <boost/test/included/unit_test.hpp>
13 
14 #include <cstdint>
15 #include <cstring>
16 
17 template <class T, int n = 1>
18 struct data {
19  typedef T value_type;
20  static const int chunk = n;
21 };
22 
23 typedef boost::mpl::list<data<double>, data<long long int>> chunk_default_data_type;
24 
25 typedef boost::mpl::list<data<double, 2>,
36 
37 BOOST_AUTO_TEST_CASE(padding_simd) {
38  /** AOS test */
39  int pad = coreneuron::soa_padded_size<1>(11, 1);
40  BOOST_CHECK_EQUAL(pad, 11);
41 
42  /** SOA tests with 11 */
43  pad = coreneuron::soa_padded_size<1>(11, 0);
44  BOOST_CHECK_EQUAL(pad, 11);
45 
46  pad = coreneuron::soa_padded_size<2>(11, 0);
47  BOOST_CHECK_EQUAL(pad, 12);
48 
49  pad = coreneuron::soa_padded_size<4>(11, 0);
50  BOOST_CHECK_EQUAL(pad, 12);
51 
52  pad = coreneuron::soa_padded_size<8>(11, 0);
53  BOOST_CHECK_EQUAL(pad, 16);
54 
55  pad = coreneuron::soa_padded_size<16>(11, 0);
56  BOOST_CHECK_EQUAL(pad, 16);
57 
58  pad = coreneuron::soa_padded_size<32>(11, 0);
59  BOOST_CHECK_EQUAL(pad, 32);
60 
61  /** SOA tests with 32 */
62  pad = coreneuron::soa_padded_size<1>(32, 0);
63  BOOST_CHECK_EQUAL(pad, 32);
64 
65  pad = coreneuron::soa_padded_size<2>(32, 0);
66  BOOST_CHECK_EQUAL(pad, 32);
67 
68  pad = coreneuron::soa_padded_size<4>(32, 0);
69  BOOST_CHECK_EQUAL(pad, 32);
70 
71  pad = coreneuron::soa_padded_size<8>(32, 0);
72  BOOST_CHECK_EQUAL(pad, 32);
73 
74  pad = coreneuron::soa_padded_size<16>(32, 0);
75  BOOST_CHECK_EQUAL(pad, 32);
76 
77  pad = coreneuron::soa_padded_size<32>(32, 0);
78  BOOST_CHECK_EQUAL(pad, 32);
79 
80  /** SOA tests with 33 */
81  pad = coreneuron::soa_padded_size<1>(33, 0);
82  BOOST_CHECK_EQUAL(pad, 33);
83 
84  pad = coreneuron::soa_padded_size<2>(33, 0);
85  BOOST_CHECK_EQUAL(pad, 34);
86 
87  pad = coreneuron::soa_padded_size<4>(33, 0);
88  BOOST_CHECK_EQUAL(pad, 36);
89 
90  pad = coreneuron::soa_padded_size<8>(33, 0);
91  BOOST_CHECK_EQUAL(pad, 40);
92 
93  pad = coreneuron::soa_padded_size<16>(33, 0);
94  BOOST_CHECK_EQUAL(pad, 48);
95 
96  pad = coreneuron::soa_padded_size<32>(33, 0);
97  BOOST_CHECK_EQUAL(pad, 64);
98 }
99 
100 /// Even number is randomly depends of the TYPE!!! and the number of elements.
101 /// This test work for 64 bits type not for 32 bits.
102 BOOST_AUTO_TEST_CASE_TEMPLATE(memory_alignment_simd_false, T, chunk_default_data_type) {
103  const int c = T::chunk;
104  int total_size_chunk = coreneuron::soa_padded_size<c>(247, 0);
105  int ne = 6 * total_size_chunk;
106 
107  typename T::value_type* data =
108  (typename T::value_type*) coreneuron::ecalloc_align(ne, sizeof(typename T::value_type), 16);
109 
110  for (int i = 1; i < 6; i += 2) {
111  bool b = coreneuron::is_aligned((data + i * total_size_chunk), 16);
112  BOOST_CHECK_EQUAL(b, 0);
113  }
114 
115  for (int i = 0; i < 6; i += 2) {
116  bool b = coreneuron::is_aligned((data + i * total_size_chunk), 16);
117  BOOST_CHECK_EQUAL(b, 1);
118  }
119 
120  free_memory(data);
121 }
122 
123 BOOST_AUTO_TEST_CASE_TEMPLATE(memory_alignment_simd_true, T, chunk_data_type) {
124  const int c = T::chunk;
125  int total_size_chunk = coreneuron::soa_padded_size<c>(247, 0);
126  int ne = 6 * total_size_chunk;
127 
128  typename T::value_type* data =
129  (typename T::value_type*) coreneuron::ecalloc_align(ne, sizeof(typename T::value_type), 16);
130 
131  for (int i = 0; i < 6; ++i) {
132  bool b = coreneuron::is_aligned((data + i * total_size_chunk), 16);
133  BOOST_CHECK_EQUAL(b, 1);
134  }
135 
136  free_memory(data);
137 }
free_memory
void free_memory(void *pointer)
Definition: memory.h:196
chunk_default_data_type
boost::mpl::list< data< double >, data< long long int > > chunk_default_data_type
Definition: alignment.cpp:23
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(padding_simd)
Definition: alignment.cpp:37
data::chunk
static const int chunk
Definition: alignment.cpp:20
data
Definition: alignment.cpp:18
i
#define i
Definition: md1redef.h:19
BOOST_AUTO_TEST_CASE_TEMPLATE
BOOST_AUTO_TEST_CASE_TEMPLATE(memory_alignment_simd_false, T, chunk_default_data_type)
Even number is randomly depends of the TYPE!!! and the number of elements.
Definition: alignment.cpp:102
chunk_data_type
boost::mpl::list< data< double, 2 >, data< double, 4 >, data< double, 8 >, data< double, 16 >, data< double, 32 >, data< int, 2 >, data< int, 4 >, data< int, 8 >, data< int, 16 >, data< int, 32 > > chunk_data_type
Definition: alignment.cpp:35
data::value_type
T value_type
Definition: alignment.cpp:19
coreneuron::ecalloc_align
void * ecalloc_align(size_t n, size_t size, size_t alignment)
memory.h