1% Another test for MULTICORE on Octave
2% By Chuong Nguyen 2007-10-12
3
4common_dir = '/tmp'; % folder for shared parameter files
5is_multiprocess = 1; % if 1, allow a secondary process
6maxMasterEvaluations = inf; % if inf, main process runs until finish
7
8%% Prepare input/output data
9N = 20;
10parameterCell = cell(1,N);
11resultCell = cell(size(parameterCell,2),1);
12for k = 1:N
13	x.a = k;
14	x.b = k;
15	x.str = 'This is a test';
16	parameterCell{1,k} = x;
17end
18
19disp('Processing with a single process')
20
21%% Single-process processing
22tic
23for k=1:size(parameterCell,2)
24	resultCell{k} = testfun_octave(parameterCell{k});
25end
26toc
27resultCell
28
29
30disp('Processing with 2 processes')
31
32resultCell = [];
33resultCell = cell(size(parameterCell,2),1);
34
35%% Open a octave process to run a secondary thread on a multicore computer
36if (is_multiprocess)
37	[in0, out0, pid0] = popen2 ("octave", "-q");
38	% fputs (in0, "startmulticoreslave('/tmp');\n");
39	input_str = ['startmulticoreslave(''',common_dir,''');'];
40	fputs (in0, input_str);
41	fputs (in0, sprintf('\n'));
42else
43	disp('Warning: no secondary process, only the main process runs. ')
44end
45
46%% Multi-process processing if possible
47tic
48resultCell = startmulticoremaster(@testfun_octave, parameterCell,...
49								 common_dir, maxMasterEvaluations);
50toc
51resultCell
52
53%% Close the secondary process
54if (is_multiprocess)
55	fclose(in0)
56	fclose(out0)
57%	pclose(pid0) % doesn't work with this software-2.9.16
58%	[err, msg] = kill (pid0, 2)
59end
60
61
62