| 1 |
#!/bin/sh |
| 2 |
# autopkgtest check: Build and run a program against libmedc |
| 3 |
# (C) 2013 Thomas Moulard |
| 4 |
# (C) 2014 Anton Gladky |
| 5 |
# Author: Thomas Moulard <[email protected]> |
| 6 |
# Anton Gladky <[email protected]> |
| 7 |
|
| 8 |
set -e |
| 9 |
|
| 10 |
WORKDIR=$(mktemp -d) |
| 11 |
echo $WORKDIR |
| 12 |
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM |
| 13 |
cd $WORKDIR |
| 14 |
mkdir src |
| 15 |
cd src |
| 16 |
|
| 17 |
cat <<EOF > CMakeLists.txt |
| 18 |
cmake_minimum_required(VERSION 3.18) |
| 19 |
project(demo) |
| 20 |
find_package(ALGLIB REQUIRED) |
| 21 |
include_directories(\${ALGLIB_INCLUDE_DIRS}) |
| 22 |
|
| 23 |
add_executable(demo demo.cpp) |
| 24 |
target_link_libraries(demo \${ALGLIB_LIB}) |
| 25 |
install(TARGETS demo DESTINATION bin) |
| 26 |
|
| 27 |
EOF |
| 28 |
|
| 29 |
cat <<EOF > demo.cpp |
| 30 |
#include "stdafx.h" |
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <math.h> |
| 34 |
#include "linalg.h" |
| 35 |
|
| 36 |
using namespace alglib; |
| 37 |
|
| 38 |
|
| 39 |
int main(int argc, char **argv) |
| 40 |
{ |
| 41 |
// |
| 42 |
// In this example we assume that you already know how to work with |
| 43 |
// rmatrixsyrk() function. Below we concentrate on its multithreading |
| 44 |
// capabilities. |
| 45 |
// |
| 46 |
// SMP edition of ALGLIB includes smp_rmatrixsyrk() - multithreaded |
| 47 |
// version of rmatrixsyrk() function. In the basic edition of ALGLIB |
| 48 |
// (GPL edition or commercial version without SMP support) this function |
| 49 |
// just calls single-threaded stub. So, you may call this function from |
| 50 |
// ANY edition of ALGLIB, but only in SMP edition it will work in really |
| 51 |
// multithreaded mode. |
| 52 |
// |
| 53 |
// In order to use multithreading, you have to: |
| 54 |
// 1) Install SMP edition of ALGLIB. |
| 55 |
// 2) This step is specific for C++ users: you should activate OS-specific |
| 56 |
// capabilities of ALGLIB by defining AE_OS=AE_POSIX (for *nix systems) |
| 57 |
// or AE_OS=AE_WINDOWS (for Windows systems). |
| 58 |
// C# users do not have to perform this step because C# programs are |
| 59 |
// portable across different systems without OS-specific tuning. |
| 60 |
// 3) Allow ALGLIB to know about number of worker threads to use: |
| 61 |
// a) autodetection (C++, C#): |
| 62 |
// ALGLIB will automatically determine number of CPU cores and |
| 63 |
// (by default) will use all cores except for one. Say, on 4-core |
| 64 |
// system it will use three cores - unless you manually told it |
| 65 |
// to use more or less. It will keep your system responsive during |
| 66 |
// lengthy computations. |
| 67 |
// Such behavior may be changed with setnworkers() call: |
| 68 |
// * alglib::setnworkers(0) = use all cores |
| 69 |
// * alglib::setnworkers(-1) = leave one core unused |
| 70 |
// * alglib::setnworkers(-2) = leave two cores unused |
| 71 |
// * alglib::setnworkers(+2) = use 2 cores (even if you have more) |
| 72 |
// b) manual specification (C++, C#): |
| 73 |
// You may want to specify maximum number of worker threads during |
| 74 |
// compile time by means of preprocessor definition AE_NWORKERS. |
| 75 |
// For C++ it will be "AE_NWORKERS=X" where X can be any positive number. |
| 76 |
// For C# it is "AE_NWORKERSX", where X should be replaced by number of |
| 77 |
// workers (AE_NWORKERS2, AE_NWORKERS3, AE_NWORKERS4, ...). |
| 78 |
// You can add this definition to compiler command line or change |
| 79 |
// corresponding project settings in your IDE. |
| 80 |
// |
| 81 |
// After you installed and configured SMP edition of ALGLIB, you may choose |
| 82 |
// between serial and multithreaded versions of SMP-capable functions: |
| 83 |
// * serial version works as usual, in the context of the calling thread |
| 84 |
// * multithreaded version (with "smp_" prefix) creates (or wakes up) worker |
| 85 |
// threads, inserts task in the worker queue, and waits for completion of |
| 86 |
// the task. All processing is done in context of worker thread(s). |
| 87 |
// |
| 88 |
// NOTE: because starting/stopping worker threads costs thousands of CPU cycles, |
| 89 |
// you should not use multithreading for lightweight computational problems. |
| 90 |
// |
| 91 |
// NOTE: some old POSIX-compatible operating systems do not support |
| 92 |
// sysconf(_SC_NPROCESSORS_ONLN) system call which is required in order |
| 93 |
// to automatically determine number of active cores. On these systems |
| 94 |
// you should specify number of cores manually at compile time. |
| 95 |
// Without it ALGLIB will run in single-threaded mode. |
| 96 |
// |
| 97 |
// Now, back to our example. In this example we will show you: |
| 98 |
// * how to call SMP version of rmatrixsyrk(). Because we work with tiny 2x2 |
| 99 |
// matrices, we won't expect to see ANY speedup from using multithreading. |
| 100 |
// The only purpose of this demo is to show how to call SMP functions. |
| 101 |
// * how to modify number of worker threads used by ALGLIB |
| 102 |
// |
| 103 |
ae_int_t n = 2; |
| 104 |
ae_int_t k = 1; |
| 105 |
double alpha = 1.0; |
| 106 |
ae_int_t ia = 0; |
| 107 |
ae_int_t ja = 0; |
| 108 |
ae_int_t optypea = 2; |
| 109 |
double beta = 0.0; |
| 110 |
ae_int_t ic = 0; |
| 111 |
ae_int_t jc = 0; |
| 112 |
bool isupper = true; |
| 113 |
real_2d_array a = "[[1,2]]"; |
| 114 |
real_2d_array c = "[[]]"; |
| 115 |
|
| 116 |
// |
| 117 |
// Default number of worker threads. |
| 118 |
// Preallocate space to store result, call multithreaded version, test. |
| 119 |
// |
| 120 |
// NOTE: this function updates only one triangular part of C. In our |
| 121 |
// example we choose to update upper triangle. |
| 122 |
// |
| 123 |
c = "[[0,0],[0,0]]"; |
| 124 |
rmatrixsyrk(n, k, alpha, a, ia, ja, optypea, beta, c, ic, jc, isupper); |
| 125 |
printf("%s\n", c.tostring(3).c_str()); // EXPECTED: [[1,2],[0,4]] |
| 126 |
|
| 127 |
// |
| 128 |
// Override default number of worker threads (set to 2). |
| 129 |
// Preallocate space to store result, call multithreaded version, test. |
| 130 |
// |
| 131 |
// NOTE: this function updates only one triangular part of C. In our |
| 132 |
// example we choose to update upper triangle. |
| 133 |
// |
| 134 |
alglib::setnworkers(+2); |
| 135 |
c = "[[0,0],[0,0]]"; |
| 136 |
rmatrixsyrk(n, k, alpha, a, ia, ja, optypea, beta, c, ic, jc, isupper); |
| 137 |
printf("%s\n", c.tostring(3).c_str()); // EXPECTED: [[1,2],[0,4]] |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
EOF |
| 143 |
|
| 144 |
cd .. |
| 145 |
mkdir build |
| 146 |
cd build |
| 147 |
cmake -DCMAKE_INSTALL_PREFIX=./../inst ./../src |
| 148 |
make |
| 149 |
make install |
| 150 |
echo "build: OK" |
| 151 |
[ -x demo ] |
| 152 |
./demo |
| 153 |
echo "run: OK" |
| 154 |
|