Select
Scenario Description
Based on the given judgment conditions, the cond tensor, and the length value, select elements from the two input tensors t and e, and store the comparison results in the output tensor. For example:
cond: [true,false,true,true] t: [5,7,15,3] e: [8,6,20,0] output: [5,6,15,3] length: 4
Code Example
#include <cmath>
#include <random>
#include <cstdint>
#include <iostream>
#include "ktfop.h"
void KtfopLog(int level, const char *message)
{
std::cout << level << ' ' << message << std::endl;
}
int main()
{
using namespace ktfop;
SetExternalLogFunc(KtfopLog);
size_t length = 4;
bool condArray[] = {true, false, true, true}; // Initialize the judgment condition array.
int64_t tArray[] = {5, 7, 15, 3}; // If the judgment conditions are true during initialization, assign a value to the output array.
int64_t eArray[] = {8, 6, 20, 0}; // If the judgment conditions are false during initialization, assign a value to the output array.
bool *cond = condArray;
int64_t *t = tArray;
int64_t *e = eArray;
auto *output = new int64_t[4]; // Allocate memory space to the output array.
int ret = -1;
ret = Select(cond, t, e, output, length); // Call the Select operator and store the results in the output array [5, 6, 15, 3].
std::cout << "output: [";
for (int i = 0; i < 4; ++i) {
std::cout << output[i];
if (i < 3) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
ret = Select(nullptr, t, e, output, length); // Print the log "ERROR Parameter verification failed for the Select Op."
delete[] output;
}
Parent topic: Examples