stream_add_pth_st.c 4.35 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 22
#include "utils.h"

23 24 25
#define ITER 10
#define CHUNKING 4

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

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

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

void *th_work(void *arg)
{
49 50

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

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

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

	/* 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();
97
		tilesz = memsize/(numthreads*CHUNKING);
98
		esz = tilesz/sizeof(unsigned long);
99
	}
100 101

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

	/* allocation */
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
116 117 118
	a = aml_area_mmap(slow, NULL, memsize);
	b = aml_area_mmap(slow, NULL, memsize);
	c = aml_area_mmap(fast, NULL, memsize);
119
	assert(a != NULL && b != NULL && c != NULL);
120

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

	/* run kernel */
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
129
	struct winfo *wis = aml_area_mmap(slow, NULL, numthreads * sizeof(struct winfo));
130 131 132 133 134 135
	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);
136
	}
Nicolas Denoyelle's avatar
Nicolas Denoyelle committed
137
	aml_area_munmap(slow, wis, numthreads * sizeof(struct winfo));
138 139 140

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

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