aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/distribution.h
blob: 1a3719c64c1234f3637af51c0c2ae1e02457c609 (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
75
76
77
#define ENTRIES_PER_BYTE(k) (UINT64_C(8) / (uint64_t)(k))
#define TABLE_SHIFT(i, k)   ((uint8_t)(k) * (uint8_t)((i) % ENTRIES_PER_BYTE(k)))
#define TABLE_MASK(i, k)    ((UINT8_BIT(k) - UINT8_C(1)) << TABLE_SHIFT(i, k))

typedef struct {
	uint64_t min;
	uint64_t max;
	uint8_t bits;
	uint64_t *distr;
	const unsigned char *table;
} getdistribution_data_t;

STATIC void *getdistribution_runthread(void *);
STATIC void getdistribution(const unsigned char *,
    uint64_t [static INFO_DISTRIBUTION_LEN], const tableinfo_t [static 1]);

STATIC void *
getdistribution_runthread(void *arg)
{
	getdistribution_data_t *data = (getdistribution_data_t *)arg;
	const unsigned char *table;
	uint8_t j, k, m;
	uint64_t i;

	memset(data->distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t));

	k = data->bits;
	table = data->table;
	m = TABLE_MASK(0, k);
	for (i = data->min; i < data->max; i++)
		for (j = 0; j < ENTRIES_PER_BYTE(k); j++)
			data->distr[(table[i] & (m << (j*k))) >> (j*k)]++;

	return NULL;
}

STATIC void
getdistribution(
	const unsigned char *table,
	uint64_t distr[static INFO_DISTRIBUTION_LEN],
	const tableinfo_t info[static 1]
) {
	getdistribution_data_t targ[THREADS];
	pthread_t thread[THREADS];
	uint8_t pval, k;
	uint64_t local_distr[THREADS][INFO_DISTRIBUTION_LEN];
	uint64_t i, j, nbytes, sz, epb;

	k = info->bits;
	epb = ENTRIES_PER_BYTE(k);
	nbytes = info->entries / epb;
	sz = nbytes / THREADS;
	for (i = 0; i < THREADS; i++) {
		targ[i] = (getdistribution_data_t) {
			.min = i * sz,
			.max = i == THREADS - 1 ? nbytes : (i+1) * sz,
			.bits = k,
			.distr = local_distr[i],
			.table = table,
		};
		pthread_create(&thread[i], NULL,
		    getdistribution_runthread, &targ[i]);
	}

	for (i = 0; i < THREADS; i++)
		pthread_join(thread[i], NULL);

	memset(distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
	for (i = 0; i < THREADS; i++)
		for (j = 0; j < INFO_DISTRIBUTION_LEN; j++)
			distr[j] += local_distr[i][j];

	for (i = nbytes * epb; i < info->entries; i++) {
		pval = (table[i/epb] & TABLE_MASK(i, k)) >> TABLE_SHIFT(i, k);
		distr[pval]++;
	}
}

Generated with cgit - Back to sebastiano.tronto.net