source: libcfa/src/concurrency/kernel/cluster.hfa @ 0deeaad

ADTast-experimental
Last change on this file since 0deeaad was 5f6b2c2, checked in by Thierry Delisle <tdelisle@…>, 19 months ago

Fixed case where the measure time could be 0 and log2(0) is a problem.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cluster.hfa -- file that includes helpers for subsystem that need cluster wide support
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Mar 15 16:40:12 2022
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#pragma once
17
18#include "device/cpu.hfa"
19#include "kernel/private.hfa"
20#include "math.hfa"
21
22#include <limits.h>
23
24#include "clock.hfa"
25
26#if   defined(READYQ_USE_LINEAR_AVG)
27
28// no conversion needed in this case
29static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) { return intsc; }
30
31// warn normally all ints
32#define warn_large_before warnf( !strict || old_avg < 33_000_000_000, "Suspiciously large previous average: %'llu (%llx), %'ldms \n", old_avg, old_avg, program()`ms )
33#define warn_large_after warnf( !strict || ret < 33_000_000_000, "Suspiciously large new average after %'ldms cputime: %'llu (%llx) from %'llu-%'llu (%'llu, %'llu) and %'llu\n", program()`ms, ret, ret, currtsc, intsc, new_val, new_val / 1000000, old_avg )
34
35// 8X linear factor is just 8 * x
36#define AVG_FACTOR( x ) (8 * (x))
37
38#elif defined(READYQ_USE_LOGDBL_AVG)
39
40// convert to log2 scale but using double
41static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) { if(unlikely(0 == intsc)) return 0.0; else return log2(intsc); }
42
43#define warn_large_before warnf( !strict || old_avg < 35.0, "Suspiciously large previous average: %'lf, %'ldms \n", old_avg, program()`ms )
44#define warn_large_after warnf( !strict || ret < 35.3, "Suspiciously large new average after %'ldms cputime: %'lf from %'llu-%'llu (%'llu, %'llu) and %'lf\n", program()`ms, ret, currtsc, intsc, new_val, new_val / 1000000, old_avg ); \
45verify(ret >= 0)
46
47// 8X factor in logscale is log2(8X) = log2(8) + log2(X) = 3 + log2(X)
48#define AVG_FACTOR( x ) (3.0 + (x))
49
50// we need to overload the __atomic_load_n because they don't support double
51static inline double __atomic_load_n(volatile double * ptr, int mem) {
52        volatile uint64_t * uptr = (volatile uint64_t *)ptr;
53        _Static_assert(sizeof(*uptr) == sizeof(*ptr));
54        uint64_t ret = 0;
55        ret = __atomic_load_n(uptr, mem);
56        uint64_t *rp = &ret;
57        double ret = *(volatile double *)rp;
58        /* paranoid */ verify( ret == 0 || ret > 3e-100 );
59        return ret;
60}
61
62// we need to overload the __atomic_store_n because they don't support double
63static inline void __atomic_store_n(volatile double * ptr, double val, int mem) {
64        /* paranoid */ verify( val == 0 || val > 3e-100 );
65        volatile uint64_t * uptr = (volatile uint64_t *)ptr;
66        _Static_assert(sizeof(*uptr) == sizeof(*ptr));
67        uint64_t * valp = (uint64_t *)&val;
68        __atomic_store_n(uptr, *valp, mem);
69}
70
71#elif defined(READYQ_USE_LOGDBL_AVG)
72
73//convert to log2 scale but with fix point u32.32 values
74static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) { return ulog2_32_32(tsc); }
75
76// 8X factor, +3 in logscale (see above) is + 0x3.00000000
77#define AVG_FACTOR( x ) (0x3_00000000ull + (x))
78
79#else
80#error must pick a scheme for averaging
81#endif
82
83//-----------------------------------------------------------------------
84// Calc moving average based on existing average, before and current time.
85static inline __readyQ_avg_t moving_average(unsigned long long currtsc, unsigned long long intsc, __readyQ_avg_t old_avg, bool strict) {
86        (void)strict; // disable the warning around the fact this is unused in release.
87        /* paranoid */ warn_large_before;
88
89        const unsigned long long new_val = currtsc > intsc ? currtsc - intsc : 0;
90        const __readyQ_avg_t total_weight = 16;
91        const __readyQ_avg_t new_weight   = 12;
92        const __readyQ_avg_t old_weight = total_weight - new_weight;
93        const __readyQ_avg_t ret = ((new_weight * __to_readyQ_avg(new_val)) + (old_weight * old_avg)) / total_weight;
94
95        /* paranoid */ warn_large_after;
96        return ret;
97}
98
99static inline void touch_tsc(__timestamp_t * tscs, size_t idx, unsigned long long ts_prev, unsigned long long ts_next, bool strict) {
100        if (ts_next == ULLONG_MAX) return;
101        unsigned long long now = rdtscl();
102        __readyQ_avg_t pma = __atomic_load_n(&tscs[ idx ].t.ma, __ATOMIC_RELAXED);
103        __atomic_store_n(&tscs[ idx ].t.tv, ts_next, __ATOMIC_RELAXED);
104        __atomic_store_n(&tscs[ idx ].t.ma, moving_average(now, ts_prev, pma, strict), __ATOMIC_RELAXED);
105}
106
107//-----------------------------------------------------------------------
108// Calc age a timestamp should be before needing help.
109forall(Data_t * | { unsigned long long ts(Data_t & this); })
110static inline __readyQ_avg_t calc_cutoff(
111        const unsigned long long ctsc,
112        unsigned procid,
113        size_t count,
114        Data_t * data,
115        __timestamp_t * tscs,
116        const unsigned shard_factor,
117        bool strict
118) {
119        unsigned start = procid;
120        __readyQ_avg_t max = 0;
121        for(i; shard_factor) {
122                unsigned long long ptsc = ts(data[start + i]);
123                if(ptsc != ULLONG_MAX) {
124                        /* paranoid */ verify( start + i < count );
125                        __readyQ_avg_t avg = moving_average(ctsc, ptsc, tscs[start + i].t.ma, strict);
126                        if(avg > max) max = avg;
127                }
128        }
129        return AVG_FACTOR( max );
130}
131
132static inline unsigned cache_id(struct cluster * cltr, unsigned idx) with (cltr->sched) {
133        // Figure out the current cpu and make sure it is valid
134        const int cpu = __kernel_getcpu();
135        /* paranoid */ verify(cpu >= 0);
136        /* paranoid */ verify(cpu < cpu_info.hthrd_count);
137        unsigned this_cache = cpu_info.llc_map[cpu].cache;
138
139        // Super important: don't write the same value over and over again
140        // We want to maximise our chances that his particular values stays in cache
141        if(caches[idx].id != this_cache)
142                __atomic_store_n(&caches[idx].id, this_cache, __ATOMIC_RELAXED);
143
144        return this_cache;
145}
146
147static struct {
148        const unsigned readyq;
149        const unsigned io;
150} __shard_factor = { 2, 1 };
151
152// Local Variables: //
153// mode: c //
154// tab-width: 4 //
155// End: //
Note: See TracBrowser for help on using the repository browser.