summaryrefslogtreecommitdiff
path: root/graphics/asymptote/parallel.cc
blob: 66bb85a342f7143a8934534fa1f9d3dcdc34db83 (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
#include <iostream>
#include <cstdlib>
#include <cstdint>

#include "parallel.h"

#ifndef __Complex_h__
#include <complex>
typedef std::complex<double> Complex;
#endif

#include "statistics.h"
#include "align.h"
#include <chrono>

namespace utils {
int ALIGNMENT=2*sizeof(Complex); // Must be a multiple of sizeof(Complex)
}

using namespace utils;

size_t threshold=SIZE_MAX;

namespace parallel {

size_t lastThreads=SIZE_MAX;

const size_t maxThreshold=1 << 24;

size_t parallelLoop(Complex *A, size_t m, size_t threads)
{
  auto T0=std::chrono::steady_clock::now();
  PARALLEL(
    for(size_t k=0; k < m; ++k)
      A[k]=k;
    );
  PARALLEL(
    for(size_t k=0; k < m; ++k)
      A[k] *= k;
    );
  auto T1=std::chrono::steady_clock::now();

  auto elapsed=std::chrono::duration_cast<std::chrono::nanoseconds>
    (T1-T0);
  return elapsed.count();
}

size_t measureThreshold(size_t threads)
{
  if(threads > 1) {
    for(size_t m=1; m < maxThreshold; m *= 2) {
      Complex *A=ComplexAlign(m);
      if(!A)
        break;
      if(parallelLoop(A,m,threads) < parallelLoop(A,m,1))
        return m;
      deleteAlign(A);
    }
  }
  return maxThreshold;
}

void Threshold(size_t threads)
{
  if(threads > 1 && threads < lastThreads) {
    statistics S(true);
    for(size_t i=0; i < 10; ++i)
      S.add(measureThreshold(threads));
    threshold=S.median();
    lastThreads=threads;
  }
}

}