dataflow_cpp_test_simple.cc 5.89 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
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
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <dataflow_cpp_test_simple.h>

#include <iostream>
#include <sstream>

32
#include <embb/tasks/tasks.h>
33 34

#include <embb/base/function.h>
35
#include <embb/base/c/memory_allocation.h>
36 37 38

#include <embb/dataflow/dataflow.h>

39 40 41
#define NUM_SLICES 8
#define TEST_COUNT 12

42
typedef embb::dataflow::Network<8> MyNetwork;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
typedef MyNetwork::ConstantSource< int > MyConstantSource;
typedef MyNetwork::Source< int > MySource;
typedef MyNetwork::SerialProcess< MyNetwork::Inputs<int>::Type,
  MyNetwork::Outputs<bool>::Type > MyPred;
typedef MyNetwork::ParallelProcess< MyNetwork::Inputs<int>::Type,
  MyNetwork::Outputs<int>::Type > MyFilter;
typedef MyNetwork::ParallelProcess< MyNetwork::Inputs<int, int>::Type,
  MyNetwork::Outputs<int>::Type > MyMult;
typedef MyNetwork::Sink< int > MySink;
typedef MyNetwork::Switch< int > MySwitch;
typedef MyNetwork::Select< int > MySelect;

embb::base::Atomic<int> source_counter;
int source_array[TEST_COUNT];

58
bool sourceFunc(int & out) {
59 60 61 62
  out = source_counter;

  source_array[source_counter] = out;
  source_counter++;
63

64
  return source_counter < TEST_COUNT;
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
}

embb::base::Atomic<int> pred_counter;
bool pred_array[TEST_COUNT];

void predFunc(int const & in, bool & out) {
  out = (0 == (in % 2));

  pred_array[pred_counter] = out;
  pred_counter++;
}

embb::base::Atomic<int> filter_counter;
int filter_array[TEST_COUNT];

void filterFunc(int const &in, int & out) {
  out = in + 1;

  filter_array[filter_counter] = out;
  filter_counter++;
}

embb::base::Atomic<int> mult_counter;
int mult_array[TEST_COUNT];

void multFunc(int const & in_a, int const & in_b, int & out) {
  out = in_a * in_b;

  mult_array[mult_counter] = out;
  mult_counter++;
}

template <int SIZE>
class ArraySink {
 private:
  int values_[SIZE];
  int pos_;

 public:
  ArraySink() {
    Init();
  }

108
  void Print() const {
109 110 111 112 113 114 115 116 117 118 119 120 121 122
    std::cout << values_[0];
    for (int ii = 1; ii < SIZE; ii++) {
      std::cout << ", " << values_[ii];
    }
    std::cout << std::endl;
  }

  void Init() {
    for (int ii = 0; ii < SIZE; ii++) {
      values_[ii] = -1;
    }
    pos_ = 0;
  }

123
  bool Check() const {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    for (int ii = 0; ii < SIZE; ii++) {
      int expected;
      if (0 == (ii % 2))
        expected = ii + 1;
      else
        expected = ii * 4;
      if (values_[ii] != expected)
        return false;
    }
    return true;
  }

  void Run(int const & in) {
    values_[pos_] = in;
    pos_++;
  }
};

SimpleTest::SimpleTest() {
  CreateUnit("dataflow_cpp simple test").Add(&SimpleTest::TestBasic, this);
}

146 147 148
#define MTAPI_DOMAIN_ID 1
#define MTAPI_NODE_ID 1

149
void SimpleTest::TestBasic() {
150 151
  // All available cores
  embb::base::CoreSet core_set(true);
152
  unsigned int num_cores = core_set.Count();
153 154 155 156 157 158
  embb::tasks::Node::Initialize(
    MTAPI_DOMAIN_ID,
    MTAPI_NODE_ID,
    core_set,
    1024, // max tasks (default: 1024)
    128,  // max groups (default: 128)
159 160 161
    // Currently needs to be initialized
    // with (max_queues + 1), see defect embb449
    num_cores + 1, // max queues (default: 16)
162
    1024, // queue capacity (default: 1024)
Tobias Fuchs committed
163
    4);   // num priorities (default: 4)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

  for (int ii = 0; ii < 10000; ii++) {
    ArraySink<TEST_COUNT> asink;
    MyNetwork network;
    MyConstantSource constant(4);
    MySource source(embb::base::MakeFunction(sourceFunc));
    MyFilter filter(embb::base::MakeFunction(filterFunc));
    MyMult mult(embb::base::MakeFunction(multFunc));
    MySink sink(embb::base::MakeFunction(asink, &ArraySink<TEST_COUNT>::Run));
    MyPred pred(embb::base::MakeFunction(predFunc));
    MySwitch sw;
    MySelect sel;

    for (int kk = 0; kk < TEST_COUNT; kk++) {
      source_array[kk] = -1;
      pred_array[kk] = false;
      filter_array[kk] = -1;
      mult_array[kk] = -1;
    }
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    source_counter = 0;
    pred_counter = 0;
    mult_counter = 0;
    filter_counter = 0;

    filter.HasInputs();
    filter.HasOutputs();

    source.GetOutput<0>() >> sw.GetInput<1>();

    source.GetOutput<0>() >> pred.GetInput<0>();
    pred.GetOutput<0>() >> sw.GetInput<0>();
    pred.GetOutput<0>() >> sel.GetInput<0>();

    sw.GetOutput<0>() >> filter.GetInput<0>();
    filter.GetOutput<0>() >> sel.GetInput<1>();

    constant.GetOutput<0>() >> mult.GetInput<0>();
    sw.GetOutput<1>() >> mult.GetInput<1>();
    mult.GetOutput<0>() >> sel.GetInput<2>();

    sel.GetOutput<0>() >> sink.GetInput<0>();

207 208
    network.AddSource(constant);
    network.AddSource(source);
209

210 211 212
    try {
      network();
    } catch (embb::base::ErrorException & e) {
213
      PT_ASSERT_MSG(false, e.What());
214
    }
215 216 217 218

    PT_EXPECT(asink.Check());
  }

219
  embb::tasks::Node::Finalize();
220 221

  PT_EXPECT(embb_get_bytes_allocated() == 0);
222
}
223