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
|
#include "HCheckConfig.h"
#if 0
#include <omp.h>
#endif
#include <iostream>
#include "catch.hpp"
#include "matrix_multiplication.hpp"
#include "parallel/HighsParallel.h"
using namespace highs;
const int numThreads = (std::thread::hardware_concurrency() + 1) / 2;
const bool dev_run = false;
int64_t fib_sequential(const int64_t n) {
if (n <= 1) return 1;
return fib_sequential(n - 1) + fib_sequential(n - 2);
}
int64_t fib(const int64_t n) {
if (n <= 20) return fib_sequential(n);
int64_t n1;
parallel::spawn([&]() { n1 = fib(n - 1); });
int64_t n2 = fib(n - 2);
parallel::sync();
// printf("fib(%ld) = %ld + %ld = %ld\n", n, n1, n2, n1 + n2);
return n1 + n2;
}
#if 0
int64_t fib_omp(const int64_t n) {
if (n <= 20) return fib_sequential(n);
int64_t n1;
#pragma omp task shared(n1)
n1 = fib_omp(n - 1);
int64_t n2 = fib_omp(n - 2);
#pragma omp taskwait
// printf("fib(%ld) = %ld + %ld = %ld\n", n, n1, n2, n1 + n2);
return n1 + n2;
}
// matrix_multiplication_omp
// reference: https://computing.llnl.gov/tutorials/openMP/samples/C/omp_mm.c
void matrix_multiplication_omp(unsigned nthreads) {
omp_set_num_threads(nthreads);
int i, j, k;
#pragma omp parallel for private(i, j)
for (i = 0; i < N; ++i) {
for (j = 0; j < N; j++) {
a[i][j] = i + j;
}
}
#pragma omp parallel for private(i, j)
for (i = 0; i < N; ++i) {
for (j = 0; j < N; j++) {
b[i][j] = i * j;
}
}
#pragma omp parallel for private(i, j)
for (i = 0; i < N; ++i) {
for (j = 0; j < N; j++) {
c[i][j] = 0;
}
}
#pragma omp parallel for private(i, j, k)
for (i = 0; i < N; ++i) {
for (j = 0; j < N; j++) {
for (k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
// int edge;
//#pragma omp parallel shared(a, b, c, nthreads) private(i, j, k)
//{
// #pragma omp single private(i, j)
// for(i = 0; i<N; i++) {
// #pragma omp task private(j) firstprivate(i) depend(out: edge)
// for (j=0; j<N; j++)
// a[i][j]= i+j;
// }
// #pragma omp single private(i, j)
// for(i = 0; i<N; i++) {
// #pragma omp task private(j) firstprivate(i) depend(out: edge)
// for (j=0; j<N; j++)
// b[i][j]= i*j;
// }
// #pragma omp single private(i, j)
// for(i = 0; i<N; i++) {
// #pragma omp task private(j) firstprivate(i) depend(out: edge)
// for (j=0; j<N; j++)
// c[i][j]= 0;
// }
// #pragma omp single private(i, j)
// for(i = 0; i<N; i++) {
// #pragma omp task private(j, k) firstprivate(i) depend(in: edge)
// for(j=0; j<N; j++) {
// for (k=0; k<N; k++) {
// c[i][j] += a[i][k] * b[k][j];
// }
// }
// }
//}
// std::cout << reduce_sum() << std::endl;
}
#endif
void matrix_multiplication_highs(unsigned nthreads) {
// #pragma omp parallel for private(i, j)
parallel::for_each(0, N, [&](HighsInt start, HighsInt end) {
for (int i = start; i < end; ++i) {
for (int j = 0; j < N; j++) {
a[i][j] = i + j;
}
}
});
parallel::for_each(0, N, [&](HighsInt start, HighsInt end) {
for (int i = start; i < end; ++i) {
for (int j = 0; j < N; j++) {
b[i][j] = i * j;
}
}
});
parallel::for_each(0, N, [&](HighsInt start, HighsInt end) {
for (int i = start; i < end; ++i) {
for (int j = 0; j < N; j++) {
c[i][j] = 0;
}
}
});
parallel::for_each(0, N, [&](HighsInt start, HighsInt end) {
for (int i = start; i < end; ++i) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
});
}
#if 0
std::chrono::microseconds measure_time_omp(unsigned num_threads) {
auto beg = std::chrono::high_resolution_clock::now();
matrix_multiplication_omp(num_threads);
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end - beg);
}
#endif
std::chrono::microseconds measure_time_highs(unsigned num_threads) {
auto beg = std::chrono::high_resolution_clock::now();
matrix_multiplication_highs(num_threads);
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end - beg);
}
int N = 0;
double **a = nullptr, **b = nullptr, **c = nullptr;
void matrix_multiplication(const std::string& model, const unsigned num_threads,
const unsigned num_rounds) {
if (dev_run)
std::cout << std::setw(12) << "size" << std::setw(12) << "runtime"
<< std::endl;
for (int i = 128; i <= 512; i += 128) {
N = i;
allocate_matrix();
double runtime{0.0};
for (unsigned j = 0; j < num_rounds; ++j) {
#if 0
if (model == "highs") {
runtime += measure_time_highs(num_threads).count();
} else if (model == "omp") {
runtime += measure_time_omp(num_threads).count();
} else
assert(false);
#else
runtime += measure_time_highs(num_threads).count();
#endif
}
if (dev_run)
std::cout << std::setw(12) << N << std::setw(12)
<< runtime / num_rounds / 1e3 << std::endl;
deallocate_matrix();
}
}
TEST_CASE("MatrixMultHighs", "[parallel]") {
HighsTaskExecutor::shutdown();
parallel::initialize_scheduler(numThreads);
if (dev_run) std::cout << "\nhighs workstealing for loop:" << std::endl;
matrix_multiplication("highs", parallel::num_threads(), 1);
}
TEST_CASE("FibonacciTasksHighs", "[parallel]") {
HighsTaskExecutor::shutdown();
auto beg = std::chrono::high_resolution_clock::now();
parallel::initialize_scheduler(numThreads);
int64_t result = fib(41);
auto end = std::chrono::high_resolution_clock::now();
if (dev_run)
std::cout << "time elapsed for fib(41) with HiGHS work stealing: "
<< (std::chrono::duration_cast<std::chrono::microseconds>(end -
beg)
.count() /
1e3)
<< "ms" << std::endl;
// REQUIRE(result == 4807526976);
// REQUIRE(result == 20365011074);
// fib 41
REQUIRE(result == 267914296);
}
#if 0
TEST_CASE("MatrixMultOmp", "[parallel]") {
if (dev_run)
std::cout << "\nomp for loop:" << std::endl;
matrix_multiplication("omp", numThreads, 1);
}
TEST_CASE("FibonacciTasksOmp", "[parallel]") {
int64_t result;
auto beg = std::chrono::high_resolution_clock::now();
#pragma omp parallel num_threads(numThreads)
{
#pragma omp single
{ result = fib_omp(41); }
}
auto end = std::chrono::high_resolution_clock::now();
if (dev_run)
std::cout << "time elapsed for fib(41) with omp tasks: "
<< (std::chrono::duration_cast<std::chrono::microseconds>(end - beg)
.count() / 1e3)
<< "ms" << std::endl;
// fib 41
REQUIRE(result == 267914296);
}
#endif
|