stream_add_pth_st.c 4.22 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the AML project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/aml
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/

11 12 13 14 15 16
#include <assert.h>
#include <errno.h>
#include <omp.h>
#include <pthread.h>
#include <stdlib.h>

17
#include "aml.h"
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
18
#include "aml/area/linux.h"
19
#include "aml/dma/linux-seq.h"
20
#include "aml/scratch/par.h"
21
#include "aml/tiling/1d.h"
22 23
#include "utils.h"

24 25 26
#define ITER 10
#define CHUNKING 4

27 28
size_t numthreads, tilesz, esz;
unsigned long *a, *b, *c;
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
29
struct aml_area *slow, *fast;
30 31 32
struct aml_tiling *tiling;
struct aml_scratch *sa;
struct aml_scratch *sb;
33 34 35 36

int kernel(unsigned long *a, unsigned long *b, unsigned long *c, size_t n)
{
	size_t i;
37
	debug("%p = %p + %p [%zi]\n",c,a,b,n);
38 39 40 41 42 43
	for(i = 0; i < n; i++)
		c[i] = a[i] + b[i];
	return 0;
}

struct winfo {
44 45
	int tid;
	pthread_t th;
46 47 48 49
};

void *th_work(void *arg)
{
50 51

	int offset, i, ai, bi, oldai, oldbi;
52
	unsigned long *ap, *bp, *cp;
53
	void *abaseptr, *bbaseptr;
54
	struct winfo *wi = arg;
55
	offset = wi->tid*CHUNKING;
56 57 58 59 60
	ap = aml_tiling_tilestart(tiling, a, offset);
	bp = aml_tiling_tilestart(tiling, b, offset);
	cp = aml_tiling_tilestart(tiling, c, offset);
	abaseptr = aml_scratch_baseptr(sa);
	bbaseptr = aml_scratch_baseptr(sb);
61 62 63 64
	ai = -1; bi = -1;
	for(i = 0; i < CHUNKING-1; i++) {
		struct aml_scratch_request *ar, *br;
		oldai = ai; oldbi = bi;
65 66
		aml_scratch_async_pull(sa, &ar, abaseptr, &ai, a, offset+i+1);
		aml_scratch_async_pull(sb, &br, bbaseptr, &bi, b, offset+i+1);
67
		kernel(ap, bp, cp, esz);
68 69 70 71 72 73 74
		aml_scratch_wait(sa, ar);
		aml_scratch_wait(sb, br);
		ap = aml_tiling_tilestart(tiling, abaseptr, ai);
		bp = aml_tiling_tilestart(tiling, bbaseptr, bi);
		cp = aml_tiling_tilestart(tiling, c, offset+i+1);
		aml_scratch_release(sa, oldai);
		aml_scratch_release(sb, oldbi);
75
	}
76
	kernel(ap, bp, cp, esz);
77 78 79 80 81

	return arg;
}
int main(int argc, char *argv[])
{
82
	struct aml_dma *dma;
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
83
	struct aml_bitmap slowb, fastb;
84
	aml_init(&argc, &argv);
85 86 87
	assert(argc == 4);

	log_init(argv[0]);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
88 89
	assert(aml_bitmap_from_string(&fastb, argv[1]) == 0);
	assert(aml_bitmap_from_string(&slowb, argv[2]) == 0);
90
	unsigned long memsize = 1UL << atoi(argv[3]);
91 92 93 94 95 96 97

	/* use openmp env to figure out how many threads we want
	 * (we actually use 3x as much)
	 */
	#pragma omp parallel
	{
		numthreads = omp_get_num_threads();
98
		tilesz = memsize/(numthreads*CHUNKING);
99
		esz = tilesz/sizeof(unsigned long);
100
	}
101 102

	/* initialize all the supporting struct */
103
	assert(!aml_tiling_1d_create(&tiling, tilesz, memsize));
104
	aml_area_linux_create(&slow, &slowb, AML_AREA_LINUX_POLICY_BIND);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
105
	assert(slow != NULL);
106
	aml_area_linux_create(&fast, &fastb, AML_AREA_LINUX_POLICY_BIND);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
107
	assert(fast != NULL);
108 109
	assert(!aml_dma_linux_seq_create(dma, (size_t)numthreads*4));
	assert(!aml_scratch_par_create(&sa, fast, slow, dma, tiling,
110
				     (size_t)2*numthreads, (size_t)numthreads));
111
	assert(!aml_scratch_par_create(&sb, fast, slow, dma, tiling,
112
				     (size_t)2*numthreads, (size_t)numthreads));
113 114

	/* allocation */
115 116 117
	a = aml_area_mmap(slow, memsize, NULL);
	b = aml_area_mmap(slow, memsize, NULL);
	c = aml_area_mmap(fast, memsize, NULL);
118
	assert(a != NULL && b != NULL && c != NULL);
119

120
	unsigned long esize = memsize/sizeof(unsigned long);
121
	for(unsigned long i = 0; i < esize; i++) {
122 123 124
		a[i] = i;
		b[i] = esize - i;
		c[i] = 0;
125 126 127
	}

	/* run kernel */
128
	struct winfo *wis = aml_area_mmap(slow, numthreads * sizeof(struct winfo), NULL);
129 130 131 132 133 134
	for(unsigned long i = 0; i < numthreads; i++) {
		wis[i].tid = i;
		pthread_create(&wis[i].th, NULL, &th_work, (void*)&wis[i]);
	}
	for(unsigned long j = 0; j < numthreads; j++) {
		pthread_join(wis[j].th, NULL);
135
	}
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
136
	aml_area_munmap(slow, wis, numthreads * sizeof(struct winfo));
137 138 139

	/* validate */
	for(unsigned long i = 0; i < esize; i++) {
140
		assert(c[i] == esize);
141 142
	}

143 144 145
	aml_scratch_par_destroy(&sa);
	aml_scratch_par_destroy(&sb);
	aml_dma_linux_seq_destroy(&dma);
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
146 147 148
	aml_area_munmap(slow, a, memsize);
	aml_area_munmap(slow, b, memsize);
	aml_area_munmap(fast, c, memsize);
149 150
	aml_area_linux_destroy(&slow);
	aml_area_linux_destroy(&fast);
151
	aml_tiling_1d_destroy(&tiling);
152 153 154
	aml_finalize();
	return 0;
}