diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..fdde62b16 --- /dev/null +++ b/.clang-format @@ -0,0 +1,13 @@ +--- +Language: Cpp +BasedOnStyle: Google + +ColumnLimit: 80 +NamespaceIndentation: Inner + +# Scaled by a factor of 2 such that the base indent is 4 +AccessModifierOffset: -3 +IndentWidth: 4 +ContinuationIndentWidth: 8 +ConstructorInitializerIndentWidth: 8 +... diff --git a/dimod/include/dimod/adjarraybqm.h b/dimod/include/dimod/adjarraybqm.h index 9f047920c..8f421c910 100644 --- a/dimod/include/dimod/adjarraybqm.h +++ b/dimod/include/dimod/adjarraybqm.h @@ -23,7 +23,7 @@ namespace dimod { -template +template class AdjArrayBQM { public: using bias_type = B; @@ -32,7 +32,8 @@ class AdjArrayBQM { using size_type = std::size_t; using outvars_iterator = typename std::vector>::iterator; - using const_outvars_iterator = typename std::vector>::const_iterator; + using const_outvars_iterator = + typename std::vector>::const_iterator; // in the future we'd probably like to make this protected std::vector> invars; @@ -41,10 +42,10 @@ class AdjArrayBQM { AdjArrayBQM() {} // can we specify this slightly better? - template - explicit AdjArrayBQM(const BQM &bqm) { + template + explicit AdjArrayBQM(const BQM& bqm) { invars.reserve(bqm.num_variables()); - outvars.reserve(2*bqm.num_interactions()); + outvars.reserve(2 * bqm.num_interactions()); for (auto v = 0; v < bqm.num_variables(); ++v) { invars.emplace_back(outvars.size(), bqm.linear(v)); @@ -59,9 +60,9 @@ class AdjArrayBQM { * * @param dense An array containing the biases. Assumed to contain * `num_variables`^2 elements. The upper and lower triangle are summed. - * @param num_variables The number of variables. + * @param num_variables The number of variables. */ - template + template AdjArrayBQM(const B2 dense[], size_type num_variables, bool ignore_diagonal = false) { // we know how big our linear is going to be. We'd also like to @@ -75,35 +76,30 @@ class AdjArrayBQM { if (ignore_diagonal) { invars.emplace_back(outvars.size(), 0); } else { - invars.emplace_back(outvars.size(), dense[u*(num_variables+1)]); + invars.emplace_back(outvars.size(), + dense[u * (num_variables + 1)]); } for (size_type v = 0; v < num_variables; ++v) { if (u == v) continue; // already did linear - qbias = dense[u*num_variables+v] + dense[v*num_variables+u]; + qbias = dense[u * num_variables + v] + + dense[v * num_variables + u]; - if (qbias != 0) - outvars.emplace_back(v, qbias); + if (qbias != 0) outvars.emplace_back(v, qbias); } } } - size_type num_interactions() const { - return outvars.size() / 2; - } + size_type num_interactions() const { return outvars.size() / 2; } - size_type num_variables() const { - return invars.size(); - } + size_type num_variables() const { return invars.size(); } - [[deprecated("Use AdjArrayBQM::linear(v)")]] - bias_type get_linear(variable_type v) const { - return linear(v); - } + [[deprecated("Use AdjArrayBQM::linear(v)")]] bias_type get_linear( + variable_type v) const { return linear(v); } - std::pair - get_quadratic(variable_type u, variable_type v) const { + std::pair get_quadratic(variable_type u, + variable_type v) const { assert(u >= 0 && u < invars.size()); assert(v >= 0 && v < invars.size()); assert(u != v); @@ -121,10 +117,10 @@ class AdjArrayBQM { assert(v >= 0 && v < invars.size()); // need to check the case that v is the last variable - if ((unsigned) v == invars.size() - 1) + if ((unsigned)v == invars.size() - 1) return outvars.size() - invars[v].first; - return invars[v+1].first - invars[v].first; + return invars[v + 1].first - invars[v].first; } bias_type& linear(variable_type v) { @@ -137,29 +133,29 @@ class AdjArrayBQM { return invars[v].second; } - std::pair - neighborhood(variable_type u) { + std::pair neighborhood( + variable_type u) { assert(u >= 0 && u < invars.size()); outvars_iterator end; - if ((unsigned) u == invars.size() - 1) { + if ((unsigned)u == invars.size() - 1) { end = outvars.end(); } else { - end = outvars.begin() + invars[u+1].first; + end = outvars.begin() + invars[u + 1].first; } return std::make_pair(outvars.begin() + invars[u].first, end); } - std::pair - neighborhood(variable_type u) const { + std::pair neighborhood( + variable_type u) const { assert(u >= 0 && u < invars.size()); const_outvars_iterator end; - if ((unsigned) u == invars.size() - 1) { + if ((unsigned)u == invars.size() - 1) { end = outvars.cend(); } else { - end = outvars.cbegin() + invars[u+1].first; + end = outvars.cbegin() + invars[u + 1].first; } return std::make_pair(outvars.cbegin() + invars[u].first, end); @@ -167,7 +163,7 @@ class AdjArrayBQM { /** * The neighborhood of variable `v`. - * + * * @param A variable `v`. * @param The neighborhood will start with the first out variable that * does not compare less than `start`. @@ -175,16 +171,16 @@ class AdjArrayBQM { * @returns A pair of iterators pointing to the start and end of the * neighborhood. */ - std::pair - neighborhood(variable_type v, variable_type start) const { + std::pair neighborhood( + variable_type v, variable_type start) const { auto span = neighborhood(v); - auto low = std::lower_bound(span.first, span.second, - start, utils::comp_v); + auto low = std::lower_bound(span.first, span.second, start, + utils::comp_v); return std::make_pair(low, span.second); - } + } - [[deprecated("Use AdjArrayBQM::linear(v)")]] - void set_linear(variable_type v, bias_type b) { + [[deprecated("Use AdjArrayBQM::linear(v)")]] void set_linear( + variable_type v, bias_type b) { assert(v >= 0 && v < invars.size()); linear(v) = b; } diff --git a/dimod/include/dimod/adjmapbqm.h b/dimod/include/dimod/adjmapbqm.h index ad4f8fd8a..3e3cad544 100644 --- a/dimod/include/dimod/adjmapbqm.h +++ b/dimod/include/dimod/adjmapbqm.h @@ -15,6 +15,7 @@ #ifndef DIMOD_ADJMAPBQM_H_ #define DIMOD_ADJMAPBQM_H_ +#include #include #include #include @@ -23,7 +24,7 @@ namespace dimod { -template +template class AdjMapBQM { public: using bias_type = B; @@ -38,7 +39,7 @@ class AdjMapBQM { AdjMapBQM() {} - template + template explicit AdjMapBQM(const BQM &bqm) { adj.resize(bqm.num_variables()); @@ -55,9 +56,9 @@ class AdjMapBQM { * * @param dense An array containing the biases. Assumed to contain * `num_variables`^2 elements. The upper and lower triangle are summed. - * @param num_variables The number of variables. + * @param num_variables The number of variables. */ - template + template AdjMapBQM(const B2 dense[], size_type num_variables, bool ignore_diagonal = false) { // we know how big our linear is going to be @@ -67,13 +68,14 @@ class AdjMapBQM { if (!ignore_diagonal) { for (size_type v = 0; v < num_variables; ++v) { - adj[v].second = dense[v*(num_variables+1)]; + adj[v].second = dense[v * (num_variables + 1)]; } } for (size_type u = 0; u < num_variables; ++u) { for (size_type v = u + 1; v < num_variables; ++v) { - qbias = dense[u*num_variables+v] + dense[v*num_variables+u]; + qbias = dense[u * num_variables + v] + + dense[v * num_variables + u]; if (qbias != 0) { adj[u].first.emplace_hint(adj[u].first.end(), v, qbias); @@ -95,108 +97,110 @@ class AdjMapBQM { template AdjMapBQM(B2 dense[], size_type num_variables, bool ignore_diagonal = false) { - // we know how big our linear is going to be - adj.resize(num_variables); + // we know how big our linear is going to be + adj.resize(num_variables); - // Backup copy of the diagonal of the dense matrix. - std::vector dense_diagonal(num_variables); + // Backup copy of the diagonal of the dense matrix. + std::vector dense_diagonal(num_variables); - if (!ignore_diagonal) { - #pragma omp parallel for - for (size_type v = 0; v < num_variables; ++v) { - adj[v].second = dense[v * (num_variables + 1)]; - } - } - - #pragma omp parallel - { - // Zero out the diagonal to avoid expensive checks inside innermost - // loop in the code for reading the matrix. The diagonal will be - // restored so a backup copy is saved. - #pragma omp for schedule(static) - for (size_type v = 0; v < num_variables; ++v) { - dense_diagonal[v] = dense[v * (num_variables + 1)]; - dense[v * (num_variables + 1)] = 0; + if (!ignore_diagonal) { +#pragma omp parallel for + for (size_type v = 0; v < num_variables; ++v) { + adj[v].second = dense[v * (num_variables + 1)]; + } } - size_type counters[BLOCK_SIZE] = {0}; - size_type buffer_size = num_variables * BLOCK_SIZE * - sizeof(std::pair); - std::pair *temp_buffer = - (std::pair *)malloc(buffer_size); +#pragma omp parallel + { +// Zero out the diagonal to avoid expensive checks inside innermost +// loop in the code for reading the matrix. The diagonal will be +// restored so a backup copy is saved. +#pragma omp for schedule(static) + for (size_type v = 0; v < num_variables; ++v) { + dense_diagonal[v] = dense[v * (num_variables + 1)]; + dense[v * (num_variables + 1)] = 0; + } - if (temp_buffer == NULL) { - printf("Memory allocation failure.\n"); - exit(0); - } + size_type counters[BLOCK_SIZE] = {0}; + size_type buffer_size = num_variables * BLOCK_SIZE * + sizeof(std::pair); + std::pair *temp_buffer = + (std::pair *)malloc(buffer_size); - // We process the matrix in blocks of size BLOCK_SIZE*BLOCK_SIZE to take - // advantage of cache locality. Dynamic scheduling is used as we know some - // blocks may be more sparse than others and processing them may finish earlier. - #pragma omp for schedule(dynamic) - for (size_type u_st = 0; u_st < num_variables; u_st += BLOCK_SIZE) { - size_type u_end = std::min(u_st + BLOCK_SIZE, num_variables); - for (size_type v_st = 0; v_st < num_variables; v_st += BLOCK_SIZE) { - size_type v_end = std::min(v_st + BLOCK_SIZE, num_variables); - for (size_type u = u_st, n = 0; u < u_end; u++, n++) { - size_type counter_u = counters[n]; - size_type counter_u_old = counter_u; - for (size_type v = v_st; v < v_end; v++) { - bias_type qbias = - dense[u * num_variables + v] + dense[v * num_variables + u]; - if (qbias != 0) { - // Even though an intermediate buffer is not needed in case of this - // model of bqm, since we cannot preallocate a map using the number - // of elements in the buffer, inserting into the map directly here - // nullifies the benefits of cache blocking due to reallocation of - // the map causing cache pollution. - temp_buffer[n * num_variables + counter_u++] = {v, qbias}; - } - } - if (counter_u != counter_u_old) { - counters[n] = counter_u; - } + if (temp_buffer == NULL) { + printf("Memory allocation failure.\n"); + exit(0); } - } - - for (size_type n = 0; n < BLOCK_SIZE; n++) { - if (counters[n]) { - std::copy(temp_buffer + n * num_variables, - temp_buffer + n * num_variables + counters[n], - std::inserter(adj[u_st + n].first, adj[u_st +n].first.begin())); - counters[n] = 0; + +// We process the matrix in blocks of size BLOCK_SIZE*BLOCK_SIZE to take +// advantage of cache locality. Dynamic scheduling is used as we know some +// blocks may be more sparse than others and processing them may finish earlier. +#pragma omp for schedule(dynamic) + for (size_type u_st = 0; u_st < num_variables; u_st += BLOCK_SIZE) { + size_type u_end = std::min(u_st + BLOCK_SIZE, num_variables); + for (size_type v_st = 0; v_st < num_variables; + v_st += BLOCK_SIZE) { + size_type v_end = + std::min(v_st + BLOCK_SIZE, num_variables); + for (size_type u = u_st, n = 0; u < u_end; u++, n++) { + size_type counter_u = counters[n]; + size_type counter_u_old = counter_u; + for (size_type v = v_st; v < v_end; v++) { + bias_type qbias = dense[u * num_variables + v] + + dense[v * num_variables + u]; + if (qbias != 0) { + // Even though an intermediate buffer is not + // needed in case of this model of bqm, since we + // cannot preallocate a map using the number of + // elements in the buffer, inserting into the + // map directly here nullifies the benefits of + // cache blocking due to reallocation of the map + // causing cache pollution. + temp_buffer[n * num_variables + counter_u++] = { + v, qbias}; + } + } + if (counter_u != counter_u_old) { + counters[n] = counter_u; + } + } + } + + for (size_type n = 0; n < BLOCK_SIZE; n++) { + if (counters[n]) { + std::copy(temp_buffer + n * num_variables, + temp_buffer + n * num_variables + counters[n], + std::inserter(adj[u_st + n].first, + adj[u_st + n].first.begin())); + counters[n] = 0; + } + } } - } - } - free(temp_buffer); + free(temp_buffer); - // Restore the diagonal of the original dense matrix - #pragma omp for schedule(static) - for (size_type v = 0; v < num_variables; ++v) { - dense[v * (num_variables + 1)] = dense_diagonal[v]; +// Restore the diagonal of the original dense matrix +#pragma omp for schedule(static) + for (size_type v = 0; v < num_variables; ++v) { + dense[v * (num_variables + 1)] = dense_diagonal[v]; + } } - } } /// Add one (disconnected) variable to the BQM and return its index. variable_type add_variable() { - adj.resize(adj.size()+1); - return adj.size()-1; + adj.resize(adj.size() + 1); + return adj.size() - 1; } /// Get the degree of variable `v`. - size_type degree(variable_type v) const { - return adj[v].first.size(); - } + size_type degree(variable_type v) const { return adj[v].first.size(); } - [[deprecated("Use AdjMapBQM::linear(v)")]] - bias_type get_linear(variable_type v) const { - return linear(v); - } + [[deprecated("Use AdjMapBQM::linear(v)")]] bias_type get_linear( + variable_type v) const { return linear(v); } - std::pair - get_quadratic(variable_type u, variable_type v) const { + std::pair get_quadratic(variable_type u, + variable_type v) const { assert(u >= 0 && u < adj.size()); assert(v >= 0 && v < adj.size()); assert(u != v); @@ -209,31 +213,31 @@ class AdjMapBQM { return std::make_pair(it->second, true); } - bias_type& linear(variable_type v) { + bias_type &linear(variable_type v) { assert(v >= 0 && v < adj.size()); return adj[v].second; } - const bias_type& linear(variable_type v) const { + const bias_type &linear(variable_type v) const { assert(v >= 0 && v < adj.size()); return adj[v].second; } - std::pair - neighborhood(variable_type u) { + std::pair neighborhood( + variable_type u) { assert(u >= 0 && u < adj.size()); return std::make_pair(adj[u].first.begin(), adj[u].first.end()); } - std::pair - neighborhood(variable_type u) const { + std::pair neighborhood( + variable_type u) const { assert(u >= 0 && u < adj.size()); return std::make_pair(adj[u].first.cbegin(), adj[u].first.cend()); } /** * The neighborhood of variable `v`. - * + * * @param A variable `v`. * @param The neighborhood will start with the first out variable that * does not compare less than `start`. @@ -241,15 +245,13 @@ class AdjMapBQM { * @returns A pair of iterators pointing to the start and end of the * neighborhood. */ - std::pair - neighborhood(variable_type v, variable_type start) const { + std::pair neighborhood( + variable_type v, variable_type start) const { return std::make_pair(adj[v].first.lower_bound(start), adj[v].first.cend()); } - size_type num_variables() const { - return adj.size(); - } + size_type num_variables() const { return adj.size(); } size_type num_interactions() const { size_type count = 0; @@ -284,8 +286,8 @@ class AdjMapBQM { return false; } - [[deprecated("Use AdjMapBQM::linear(v)")]] - void set_linear(variable_type v, bias_type b) { + [[deprecated("Use AdjMapBQM::linear(v)")]] void set_linear(variable_type v, + bias_type b) { assert(v >= 0 && v < adj.size()); linear(v) = b; } diff --git a/dimod/include/dimod/adjvectorbqm.h b/dimod/include/dimod/adjvectorbqm.h index f7153ab56..012233190 100644 --- a/dimod/include/dimod/adjvectorbqm.h +++ b/dimod/include/dimod/adjvectorbqm.h @@ -15,8 +15,8 @@ #ifndef DIMOD_ADJVECTORBQM_H_ #define DIMOD_ADJVECTORBQM_H_ -#include #include +#include #include #include @@ -24,7 +24,7 @@ namespace dimod { -template +template class AdjVectorBQM { public: using bias_type = B; @@ -32,14 +32,15 @@ class AdjVectorBQM { using size_type = std::size_t; using outvars_iterator = typename std::vector>::iterator; - using const_outvars_iterator = typename std::vector>::const_iterator; + using const_outvars_iterator = + typename std::vector>::const_iterator; // in the future we'd probably like to make this protected std::vector>, B>> adj; AdjVectorBQM() {} - template + template explicit AdjVectorBQM(const BQM &bqm) { adj.resize(bqm.num_variables()); @@ -56,9 +57,9 @@ class AdjVectorBQM { * * @param dense An array containing the biases. Assumed to contain * `num_variables`^2 elements. The upper and lower triangle are summed. - * @param num_variables The number of variables. + * @param num_variables The number of variables. */ - template + template AdjVectorBQM(const B2 dense[], size_type num_variables, bool ignore_diagonal = false) { // we know how big our linear is going to be @@ -68,13 +69,14 @@ class AdjVectorBQM { if (!ignore_diagonal) { for (size_type v = 0; v < num_variables; ++v) { - adj[v].second = dense[v*(num_variables+1)]; + adj[v].second = dense[v * (num_variables + 1)]; } } for (size_type u = 0; u < num_variables; ++u) { for (size_type v = u + 1; v < num_variables; ++v) { - qbias = dense[u*num_variables+v] + dense[v*num_variables+u]; + qbias = dense[u * num_variables + v] + + dense[v * num_variables + u]; if (qbias != 0) { adj[u].first.emplace_back(v, qbias); @@ -96,102 +98,102 @@ class AdjVectorBQM { template AdjVectorBQM(B2 dense[], size_type num_variables, bool ignore_diagonal = false) { - // we know how big our linear is going to be - adj.resize(num_variables); + // we know how big our linear is going to be + adj.resize(num_variables); - // Backup copy of the diagonal of the dense matrix. - std::vector dense_diagonal(num_variables); + // Backup copy of the diagonal of the dense matrix. + std::vector dense_diagonal(num_variables); - if (!ignore_diagonal) { - #pragma omp parallel for - for (size_type v = 0; v < num_variables; ++v) { - adj[v].second = dense[v * (num_variables + 1)]; - } - } - - #pragma omp parallel - { - // Zero out the diagonal to avoid expensive checks inside innermost - // loop in the code for reading the matrix. The diagonal will be - // restored so a backup copy is saved. - #pragma omp for schedule(static) - for (size_type v = 0; v < num_variables; ++v) { - dense_diagonal[v] = dense[v * (num_variables + 1)]; - dense[v * (num_variables + 1)] = 0; + if (!ignore_diagonal) { +#pragma omp parallel for + for (size_type v = 0; v < num_variables; ++v) { + adj[v].second = dense[v * (num_variables + 1)]; + } } - size_type counters[BLOCK_SIZE] = {0}; - size_type buffer_size = num_variables * BLOCK_SIZE * - sizeof(std::pair); - std::pair *temp_buffer = - (std::pair *)malloc(buffer_size); +#pragma omp parallel + { +// Zero out the diagonal to avoid expensive checks inside innermost +// loop in the code for reading the matrix. The diagonal will be +// restored so a backup copy is saved. +#pragma omp for schedule(static) + for (size_type v = 0; v < num_variables; ++v) { + dense_diagonal[v] = dense[v * (num_variables + 1)]; + dense[v * (num_variables + 1)] = 0; + } - if (temp_buffer == NULL) { - printf("Memory allocation failure.\n"); - exit(0); - } + size_type counters[BLOCK_SIZE] = {0}; + size_type buffer_size = num_variables * BLOCK_SIZE * + sizeof(std::pair); + std::pair *temp_buffer = + (std::pair *)malloc(buffer_size); - // We process the matrix in blocks of size BLOCK_SIZE*BLOCK_SIZE to take - // advantage of cache locality. Dynamic scheduling is used as we know some - // blocks may be more sparse than others and processing them may finish earlier. - #pragma omp for schedule(dynamic) - for (size_type u_st = 0; u_st < num_variables; u_st += BLOCK_SIZE) { - size_type u_end = std::min(u_st + BLOCK_SIZE, num_variables); - for (size_type v_st = 0; v_st < num_variables; v_st += BLOCK_SIZE) { - size_type v_end = std::min(v_st + BLOCK_SIZE, num_variables); - for (size_type u = u_st, n = 0; u < u_end; u++, n++) { - size_type counter_u = counters[n]; - size_type counter_u_old = counter_u; - for (size_type v = v_st; v < v_end; v++) { - bias_type qbias = - dense[u * num_variables + v] + dense[v * num_variables + u]; - if (qbias != 0) { - temp_buffer[n * num_variables + counter_u++] = {v, qbias}; - } - } - if (counter_u != counter_u_old) { - counters[n] = counter_u; - } + if (temp_buffer == NULL) { + printf("Memory allocation failure.\n"); + exit(0); } - } - for (size_type n = 0; n < BLOCK_SIZE; n++) { - if (counters[n]) { - adj[u_st + n].first.assign(temp_buffer + n * num_variables, - temp_buffer + n * num_variables + counters[n]); - counters[n] = 0; +// We process the matrix in blocks of size BLOCK_SIZE*BLOCK_SIZE to take +// advantage of cache locality. Dynamic scheduling is used as we know some +// blocks may be more sparse than others and processing them may finish earlier. +#pragma omp for schedule(dynamic) + for (size_type u_st = 0; u_st < num_variables; u_st += BLOCK_SIZE) { + size_type u_end = std::min(u_st + BLOCK_SIZE, num_variables); + for (size_type v_st = 0; v_st < num_variables; + v_st += BLOCK_SIZE) { + size_type v_end = + std::min(v_st + BLOCK_SIZE, num_variables); + for (size_type u = u_st, n = 0; u < u_end; u++, n++) { + size_type counter_u = counters[n]; + size_type counter_u_old = counter_u; + for (size_type v = v_st; v < v_end; v++) { + bias_type qbias = dense[u * num_variables + v] + + dense[v * num_variables + u]; + if (qbias != 0) { + temp_buffer[n * num_variables + counter_u++] = { + v, qbias}; + } + } + if (counter_u != counter_u_old) { + counters[n] = counter_u; + } + } + } + + for (size_type n = 0; n < BLOCK_SIZE; n++) { + if (counters[n]) { + adj[u_st + n].first.assign( + temp_buffer + n * num_variables, + temp_buffer + n * num_variables + counters[n]); + counters[n] = 0; + } + } } - } - } - free(temp_buffer); + free(temp_buffer); - // Restore the diagonal of the original dense matrix - #pragma omp for schedule(static) - for (size_type v = 0; v < num_variables; ++v) { - dense[v * (num_variables + 1)] = dense_diagonal[v]; +// Restore the diagonal of the original dense matrix +#pragma omp for schedule(static) + for (size_type v = 0; v < num_variables; ++v) { + dense[v * (num_variables + 1)] = dense_diagonal[v]; + } } - } } /// Add one (disconnected) variable to the BQM and return its index. variable_type add_variable() { - adj.resize(adj.size()+1); - return adj.size()-1; + adj.resize(adj.size() + 1); + return adj.size() - 1; } /// Get the degree of variable `v`. - size_type degree(variable_type v) const { - return adj[v].first.size(); - } + size_type degree(variable_type v) const { return adj[v].first.size(); } - [[deprecated("Use AdjVectorBQM::linear(v)")]] - bias_type get_linear(variable_type v) const { - return linear(v); - } + [[deprecated("Use AdjVectorBQM::linear(v)")]] bias_type get_linear( + variable_type v) const { return linear(v); } - std::pair - get_quadratic(variable_type u, variable_type v) const { + std::pair get_quadratic(variable_type u, + variable_type v) const { assert(u >= 0 && u < adj.size()); assert(v >= 0 && v < adj.size()); assert(u != v); @@ -205,32 +207,31 @@ class AdjVectorBQM { return std::make_pair(low->second, true); } - - bias_type& linear(variable_type v) { + bias_type &linear(variable_type v) { assert(v >= 0 && v < adj.size()); return adj[v].second; } - const bias_type& linear(variable_type v) const { + const bias_type &linear(variable_type v) const { assert(v >= 0 && v < adj.size()); return adj[v].second; } - std::pair - neighborhood(variable_type u) { + std::pair neighborhood( + variable_type u) { assert(u >= 0 && u < adj.size()); return std::make_pair(adj[u].first.begin(), adj[u].first.end()); } - std::pair - neighborhood(variable_type u) const { + std::pair neighborhood( + variable_type u) const { assert(u >= 0 && u < adj.size()); return std::make_pair(adj[u].first.cbegin(), adj[u].first.cend()); } /** * The neighborhood of variable `v`. - * + * * @param A variable `v`. * @param The neighborhood will start with the first out variable that * does not compare less than `start`. @@ -238,17 +239,15 @@ class AdjVectorBQM { * @returns A pair of iterators pointing to the start and end of the * neighborhood. */ - std::pair - neighborhood(variable_type v, variable_type start) const { + std::pair neighborhood( + variable_type v, variable_type start) const { auto span = neighborhood(v); - auto low = std::lower_bound(span.first, span.second, - start, utils::comp_v); + auto low = std::lower_bound(span.first, span.second, start, + utils::comp_v); return std::make_pair(low, span.second); } - size_type num_variables() const { - return adj.size(); - } + size_type num_variables() const { return adj.size(); } size_type num_interactions() const { size_type count = 0; @@ -300,8 +299,8 @@ class AdjVectorBQM { return exists; } - [[deprecated("Use AdjVectorBQM::linear(v)")]] - void set_linear(variable_type v, bias_type b) { + [[deprecated("Use AdjVectorBQM::linear(v)")]] void set_linear( + variable_type v, bias_type b) { assert(v >= 0 && v < adj.size()); linear(v) = b; } diff --git a/dimod/include/dimod/utils.h b/dimod/include/dimod/utils.h index 64484a6e1..9548f27d6 100644 --- a/dimod/include/dimod/utils.h +++ b/dimod/include/dimod/utils.h @@ -17,15 +17,15 @@ #include -#define BLOCK_SIZE 64 // Block size for cache blocking. +#define BLOCK_SIZE 64 // Block size for cache blocking. namespace dimod { namespace utils { -template -bool comp_v(std::pair ub, V v) { - return ub.first < v; -} + template + bool comp_v(std::pair ub, V v) { + return ub.first < v; + } } // namespace utils } // namespace dimod diff --git a/testscpp/tests/test_bqm.cpp b/testscpp/tests/test_bqm.cpp index 76a2230d1..adaeb5d7f 100755 --- a/testscpp/tests/test_bqm.cpp +++ b/testscpp/tests/test_bqm.cpp @@ -15,20 +15,16 @@ #include #include "../Catch2/single_include/catch2/catch.hpp" -#include "dimod/adjvectorbqm.h" -#include "dimod/adjmapbqm.h" #include "dimod/adjarraybqm.h" +#include "dimod/adjmapbqm.h" +#include "dimod/adjvectorbqm.h" namespace dimod { -TEMPLATE_TEST_CASE("Tests for BQM Classes", - "[bqm]", - (AdjVectorBQM), (AdjMapBQM), (AdjArrayBQM)) { - +TEMPLATE_TEST_CASE("Tests for BQM Classes", "[bqm]", (AdjVectorBQM), + (AdjMapBQM), (AdjArrayBQM)) { SECTION("Test neighborhood()") { - float Q[9] = {1.0, 0.0, 3.0, - 2.0, 1.5, 6.0, - 1.0, 0.0, 0.0}; + float Q[9] = {1.0, 0.0, 3.0, 2.0, 1.5, 6.0, 1.0, 0.0, 0.0}; auto bqm = TestType(Q, 3); std::vector neighbors; @@ -81,11 +77,9 @@ TEMPLATE_TEST_CASE("Tests for BQM Classes", } SECTION("Test neighborhood() start") { - float Q[25] = {0.0, 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, 0.0}; + float Q[25] = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, + 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0}; auto bqm = TestType(Q, 5); std::vector neighbors; @@ -141,35 +135,33 @@ TEMPLATE_TEST_CASE("Tests for BQM Classes", float Q[4] = {1, 0, -1, 2}; auto bqm = TestType(Q, 2); - REQUIRE(bqm.get_linear(1) == 2); + REQUIRE(bqm.get_linear(1) == 2); } SECTION("Test get_quadratic()") { float Q[4] = {1, 0, -1, 2}; auto bqm = TestType(Q, 2); - auto q = bqm.get_quadratic(0,1); + auto q = bqm.get_quadratic(0, 1); REQUIRE(q.first == -1); REQUIRE(q.second); } } -TEMPLATE_TEST_CASE("Tests for Shapeable BQM Classes", - "[shapeablebqm][bqm]", +TEMPLATE_TEST_CASE("Tests for Shapeable BQM Classes", "[shapeablebqm][bqm]", (AdjVectorBQM), (AdjMapBQM)) { - - auto bqm = TestType(); + auto bqm = TestType(); bqm.add_variable(); SECTION("Test add_variable()") { bqm.add_variable(); - REQUIRE(bqm.num_variables() == 2); + REQUIRE(bqm.num_variables() == 2); } SECTION("Test pop_variable()") { bqm.pop_variable(); - REQUIRE(bqm.num_variables() == 0); + REQUIRE(bqm.num_variables() == 0); } } -} // namespace dimod +} // namespace dimod diff --git a/testscpp/tests/test_roofduality.cpp b/testscpp/tests/test_roofduality.cpp index ef35c1cac..9c8e41bbd 100644 --- a/testscpp/tests/test_roofduality.cpp +++ b/testscpp/tests/test_roofduality.cpp @@ -14,28 +14,24 @@ // // ============================================================================= -#include "../Catch2/single_include/catch2/catch.hpp" #include "../../dimod/roof_duality/src/fix_variables.hpp" +#include "../Catch2/single_include/catch2/catch.hpp" namespace fix_variables_ { TEST_CASE("Test roof_duality's fixQuboVariables()", "[roofduality]") { SECTION("Test invalid cases") { - auto nonsquare_matrix = compressed_matrix::CompressedMatrix(2,3); - REQUIRE_THROWS_AS( - fixQuboVariables(nonsquare_matrix, 2), - std::invalid_argument - ); + auto nonsquare_matrix = + compressed_matrix::CompressedMatrix(2, 3); + REQUIRE_THROWS_AS(fixQuboVariables(nonsquare_matrix, 2), + std::invalid_argument); - auto matrix = compressed_matrix::CompressedMatrix(2,2); - REQUIRE_THROWS_AS( - fixQuboVariables(matrix, 0), - std::invalid_argument - ); + auto matrix = compressed_matrix::CompressedMatrix(2, 2); + REQUIRE_THROWS_AS(fixQuboVariables(matrix, 0), std::invalid_argument); } SECTION("Test empty case") { - auto empty_matrix = compressed_matrix::CompressedMatrix(0,0); + auto empty_matrix = compressed_matrix::CompressedMatrix(0, 0); FixVariablesResult result = fixQuboVariables(empty_matrix, 2); REQUIRE(result.offset == 0); @@ -44,5 +40,5 @@ TEST_CASE("Test roof_duality's fixQuboVariables()", "[roofduality]") { REQUIRE(result.newQ.numRows() == 0); } } - -} // namespace fix_variables_ + +} // namespace fix_variables_