arrayinit-example-mt-ff.cc 4.37 KB
Newer Older
1
2
3
//
// Copyright (C) 2014 Jens Korinth, TU Darmstadt
//
4
// This file is part of Tapasco (TPC).
5
//
6
// Tapasco is free software: you can redistribute it and/or modify
7
8
9
10
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
11
// Tapasco is distributed in the hope that it will be useful,
12
13
14
15
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
17
// along with Tapasco.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
25
26
27
28
29
//
//! @file	arrayinit-example-mt-ff.cc
//! @brief	TPC API based example program exercising a hardware threadpool
//!             containing instances of the arrayinit kernel.
//!             Multi-threaded FastFlow variant.
//! @authors	J. Korinth, TU Darmstadt (jk@esa.cs.tu-darmstadt.de)
//!
#include <cerrno>
#include <iostream>
#include <vector>
#include <unistd.h>
#include <cassert>
30
#include <tapasco.h>
31
32
33
#include <ff/farm.hpp>
#include "arrayinit.h"
using namespace ff;
34
using namespace rpr::tapasco;
35
36
37
38

#define SZ							256
#define RUNS							25	

39
40
static tapasco_ctx_t *ctx;
static tapasco_dev_ctx_t *dev;
41
42
43
44
45
46
47
48
49
50
51
static int *arr;
int errs = 0;

static void check(int const result)
{
	if (! result) {
		std::cerr << "fatal error: " << strerror(errno) << std::endl;
		exit(errno);
	}
}

52
static void check_tapasco(tapasco_res_t const result)
53
{
54
55
	if (result != TAPASCO_SUCCESS) {
		std::cerr << "tapasco fatal error: " << tapasco_strerror(result)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
				<< std::endl;
		exit(result);
	}
}

static void init_array(int *arr, size_t sz)
{
	for (size_t i = 0; i < sz; ++i)
		arr[i] = -1;
}

unsigned int check_array(int *arr, size_t sz)
{
	unsigned int errs = 0;
	for (size_t i = 0; i < sz; ++i) {
		if (arr[i] != static_cast<int>(i)) {
			std::cerr << "wrong data at " << i << " [" << arr[i]
					<< "]" << std::endl;
			++errs;
		}
	}
	return errs;
}

static int runTest(int const run)
{
	// allocate mem on device and copy array part
83
	tapasco_handle_t h = tapasco_device_alloc(dev, SZ * sizeof(int), 0);
84
85
86
	check(h != 0);

	// get a job id and set argument to handle
87
88
	tapasco_job_id_t j_id = tapasco_device_acquire_job_id(dev, 11,
			TAPASCO_ACQUIRE_JOB_ID_BLOCKING);
89
90
	std::cout << "run " << run << ": j_id = " << j_id << std::endl;
	check(j_id > 0);
91
	check_tapasco(tapasco_device_job_set_arg(dev, j_id, 0, sizeof(h), &h));
92
93

	// shoot me to the moon!
94
	check_tapasco(tapasco_device_job_launch(dev, j_id, TAPASCO_JOB_LAUNCH_BLOCKING));
95
96

	// get the result
97
98
	check_tapasco(tapasco_device_copy_from(dev, h, &arr[SZ * run],
			SZ * sizeof(int), TAPASCO_COPY_BLOCKING));
99
100
101
	unsigned int errs = check_array(&arr[SZ * run], SZ);
	std::cout << std::endl << "RUN " << run << ": " <<
			(errs == 0 ? "OK" : "NOT OK") << std::endl;
102
103
	tapasco_device_free(dev, h);
	tapasco_device_release_job_id(dev, j_id);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	return errs;
}

struct Emitter: ff_node_t<int> {
	int *svc(int *)
	{
		for (size_t i = 0; i < RUNS; ++i)
			ff_send_out(new int(i));
		return EOS;
	}
};
struct Worker: ff_node_t<int> {
	int *svc(int *task)
	{
		int &t = *task;
		t = runTest(t);
		return task;
	}
};
struct Collector: ff_node_t<int> {
	int *svc(int *t)
	{
		__sync_fetch_and_add(&errs, *t);
		delete t;
		return GO_ON;
	}
	void svc_end() { std::cout << "Total number of errors: " << errs << std::endl; }
};

int main(int argc, char **argv)
{
	int errs = 0;

	// init threadpool
138
139
	check_tapasco(tapasco_init(&ctx));
	check_tapasco(tapasco_create_device(ctx, 0, &dev, 0));
140
	// check arrayinit instance count
141
	std::cout << "instance count: " << tapasco_device_func_instance_count(dev, 11)
142
			<< std::endl;
143
	assert(tapasco_device_func_instance_count(dev, 11));
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

	// init whole array to subsequent numbers
	arr = (int *)malloc(SZ * RUNS * sizeof(int));
	check(arr != NULL);
	init_array(arr, SZ * RUNS);

	// setup ff_farm
	std::vector<ff_node *> f;
	for (int i = 0; i < sysconf(_SC_NPROCESSORS_CONF); ++i)
		f.push_back(new Worker);
	
	Emitter e;
	Collector c;
	ff_farm<> farm(f, &e, &c);

	farm.set_scheduling_ondemand();
	farm.cleanup_workers();
	farm.run_and_wait_end();

	if (! errs) 
		std::cout << "SUCCESS" << std::endl;
	else
		std::cerr << "FAILURE" << std::endl;

	// de-initialize threadpool
169
170
	tapasco_destroy_device(ctx, dev);
	tapasco_deinit(ctx);
171
172
173
	free(arr);
	return errs;
}