stream_add_omp_mt.c 3.61 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
#include "aml.h"
12
#include "aml/area/linux.h"
13
#include "aml/dma/linux-par.h"
14
#include "aml/scratch/seq.h"
15
#include "aml/tiling/1d.h"
16 17 18 19 20 21 22
#include <assert.h>
#include <errno.h>
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

23 24
#include "utils.h"

25 26 27 28 29
#define ITER 10
#define CHUNKING 4

size_t numthreads, tilesz, esz;
unsigned long *a, *b, *c;
30
struct aml_tiling *tiling;
31
struct aml_area *slow, *fast;
32 33
struct aml_scratch *sa;
struct aml_scratch *sb;
34 35 36 37 38 39 40 41 42 43 44

int kernel(unsigned long *a, unsigned long *b, unsigned long *c, size_t n)
{
	#pragma omp parallel for
	for(size_t i = 0; i < n; i++)
		c[i] = a[i] + b[i];
	return 0;
}

int main(int argc, char *argv[])
{
45
	struct aml_dma *dma;
46
	struct aml_bitmap slowb, fastb;
47
	aml_init(&argc, &argv);
48 49 50
	assert(argc == 4);

	log_init(argv[0]);
51 52
	assert(aml_bitmap_from_string(&fastb, argv[1]) == 0);
	assert(aml_bitmap_from_string(&slowb, argv[2]) == 0);
53
	unsigned long memsize = 1UL << atoi(argv[3]);
54 55 56 57 58 59 60

	/* 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();
61
		tilesz = memsize/(numthreads*CHUNKING);
62 63 64 65
		esz = tilesz/sizeof(unsigned long);
	}

	/* initialize all the supporting struct */
66
	assert(!aml_tiling_1d_create(&tiling, tilesz, memsize));
67
	aml_area_linux_create(&slow, &slowb, AML_AREA_LINUX_POLICY_BIND);
68
	assert(slow != NULL);
69
	aml_area_linux_create(&fast, &fastb, AML_AREA_LINUX_POLICY_BIND);
70
	assert(fast != NULL);
71
	assert(!aml_dma_linux_par_create(&dma, numthreads*2, NULL, NULL));
72
	assert(!aml_scratch_seq_create(&sa, fast, slow, dma, tiling,
73
				     (size_t)2*numthreads, (size_t)1));
74
	assert(!aml_scratch_seq_create(&sb, fast, slow, dma, tiling,
75
				     (size_t)2*numthreads, (size_t)1));
76 77

	/* allocation */
78 79 80
	a = aml_area_mmap(slow, memsize, NULL);
	b = aml_area_mmap(slow, memsize, NULL);
	c = aml_area_mmap(fast, memsize, NULL);
81 82
	assert(a != NULL && b != NULL && c != NULL);

83
	unsigned long esize = memsize/sizeof(unsigned long);
84 85 86 87 88 89 90 91 92 93
	for(unsigned long i = 0; i < esize; i++) {
		a[i] = i;
		b[i] = esize - i;
		c[i] = 0;
	}

	/* run kernel */
	int i, ai, bi, oldai, oldbi;
	unsigned long *ap, *bp;
	void *abaseptr, *bbaseptr;
94 95 96 97
	ap = aml_tiling_tilestart(tiling, a, 0);
	bp = aml_tiling_tilestart(tiling, b, 0);
	abaseptr = aml_scratch_baseptr(sa);
	bbaseptr = aml_scratch_baseptr(sb);
98
	ai = -1; bi = -1;
99
	for(i = 0; i < (memsize/tilesz) -1; i++) {
100 101
		struct aml_scratch_request *ar, *br;
		oldai = ai; oldbi = bi;
102 103
		aml_scratch_async_pull(sa, &ar, abaseptr, &ai, a, i+1);
		aml_scratch_async_pull(sb, &br, bbaseptr, &bi, b, i+1);
104
		kernel(ap, bp, &c[i*esz], esz);
105 106 107 108 109 110
		aml_scratch_wait(sa, ar);
		aml_scratch_wait(sb, br);
		ap = aml_tiling_tilestart(tiling, abaseptr, ai);
		bp = aml_tiling_tilestart(tiling, bbaseptr, bi);
		aml_scratch_release(sa, oldai);
		aml_scratch_release(sb, oldbi);
111 112 113 114 115 116 117 118
	}
	kernel(ap, bp, &c[i*esz], esz);

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

119 120 121
	aml_scratch_seq_destroy(&sa);
	aml_scratch_seq_destroy(&sb);
	aml_dma_linux_par_destroy(&dma);
122 123 124
	aml_area_munmap(slow, a, memsize);
	aml_area_munmap(slow, b, memsize);
	aml_area_munmap(fast, c, memsize);
125 126
	aml_area_linux_destroy(&slow);
	aml_area_linux_destroy(&fast);
127
	aml_tiling_1d_destroy(&tiling);
128 129 130
	aml_finalize();
	return 0;
}