arrayinit-example-mt-ff.cc 4.29 KB
Newer Older
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
138
139
140
141
142
143
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
169
170
171
172
173
//
// Copyright (C) 2014 Jens Korinth, TU Darmstadt
//
// This file is part of ThreadPoolComposer (TPC).
//
// ThreadPoolComposer is free software: you can redistribute it and/or modify
// 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.
//
// ThreadPoolComposer is distributed in the hope that it will be useful,
// 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
// along with ThreadPoolComposer.  If not, see <http://www.gnu.org/licenses/>.
//
//! @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>
#include <tpc_api.h>
#include <ff/farm.hpp>
#include "arrayinit.h"
using namespace ff;
using namespace rpr::tpc;

#define SZ							256
#define RUNS							25	

static tpc_ctx_t *ctx;
static tpc_dev_ctx_t *dev;
static int *arr;
int errs = 0;

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

static void check_tpc(tpc_res_t const result)
{
	if (result != TPC_SUCCESS) {
		std::cerr << "tpc fatal error: " << tpc_strerror(result)
				<< 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
	tpc_handle_t h = tpc_device_alloc(dev, SZ * sizeof(int), 0);
	check(h != 0);

	// get a job id and set argument to handle
	tpc_job_id_t j_id = tpc_device_acquire_job_id(dev, 11,
			TPC_ACQUIRE_JOB_ID_BLOCKING);
	std::cout << "run " << run << ": j_id = " << j_id << std::endl;
	check(j_id > 0);
	check_tpc(tpc_device_job_set_arg(dev, j_id, 0, sizeof(h), &h));

	// shoot me to the moon!
	check_tpc(tpc_device_job_launch(dev, j_id, TPC_JOB_LAUNCH_BLOCKING));

	// get the result
	check_tpc(tpc_device_copy_from(dev, h, &arr[SZ * run],
			SZ * sizeof(int), TPC_COPY_BLOCKING));
	unsigned int errs = check_array(&arr[SZ * run], SZ);
	std::cout << std::endl << "RUN " << run << ": " <<
			(errs == 0 ? "OK" : "NOT OK") << std::endl;
	tpc_device_free(dev, h);
	tpc_device_release_job_id(dev, j_id);
	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
	check_tpc(tpc_init(&ctx));
	check_tpc(tpc_create_device(ctx, 0, &dev, 0));
	// check arrayinit instance count
	std::cout << "instance count: " << tpc_device_func_instance_count(dev, 11)
			<< std::endl;
	assert(tpc_device_func_instance_count(dev, 11));

	// 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
	tpc_destroy_device(ctx, dev);
	tpc_deinit(ctx);
	free(arr);
	return errs;
}