I'm trying to get started working with Intel's Array Building Blocks, and there seems to only be one tutorial on "Hello World", at http://www.hpc.lsu.edu/training/tutorials/sc10/tutorials/SC10Tutorials/docs/M07/M07.pdf . And I'm not really getting it.
I'm using Visual Studio 2010 and this is the code I got from the above link, kinda.
#include <C:/Program Files/intel/arbb/Beta6/include/arbb.hpp>
//What do I have to do to make just "#include <arbb.hpp>" work?
using namespace arbb;
void my_function(f32& result, f32 input){
std::cout << "Hello, world!" << std::endl;
result = input + 1.0f; //"Error: no operator "+" matches these operands
}
int main(){
typedef closure<void (f32&, f32)> mfc;
mfc a = capture(my_function);
mfc b = call(my_function);
mfc c = call(my_function);
}
What else do I need to do to get "Hello World" working?
There are many samples available in arbb installation path. You can use the visual studio solution files to start with any of the sample. That is the easiest way.
In order to compile and run your own application from scratch, you have to have the include and dependencies set.
On Linux, you can add the path ~/(whatever)/intel/arbb/Beta6/include in the compile option using -I
On Windows, you can do:
set INCLUDE=C:/Program Files/intel/arbb/Beta6/include/arbb.hpp;
Or have a batch script that will ensure all the environment variables are set by default.
--- contents of the batch file ---
SET ARBB_OPT_LEVEL=O3
SET PATH=%ARBB_ROOT%\bin\ia32;%ARBB_ROOT%\bin\ia32\vs%MSVS_VERSION%;%OPENCV_ROOT%\bin;%FFTW_ROOT%;%FREEGLUT_ROOT%;%PTHREADS_ROOT%\lib;%PATH%
---- here is hello world program in arbb ---
#include <arbb.hpp>
void arbb_hello_map(arbb::i32& val)
{
val = val * 2;
}
void arbb_hello(arbb::dense<arbb::i32>& data)
{
using namespace arbb;
map(arbb_hello_map)(data);
}
int main()
{
using namespace arbb;
int size = 5;
dense<i32> data = dense<i32>(size);
range<i32> write_data = data.write_only_range();
for (int i = 0; i < size; ++i)
write_data[i] = i;
arbb::call(arbb_hello)(data);
std::cout << "hello: " << std::endl;
const_range<i32> read_data = data.read_only_range();
for (int i = 0; i < size; ++i)
std::cout <<"data["<<i<<"] = " << read_data[i] <<"\n";
return 0;
}
And compile it using
g++ -m64 -I/home/YOUR_NAME/arbb/install//include -Wall -Werror -O3 -W -Wshadow temp.cpp -o temp -L/home/YOUR_NAME/arbb/install/lib/intel64 -larbb_dev -ltbb -littnotify
Run it using
./temp
Related
I want to install all I need to use libp11 and use libp11.
What is my environment and needs:
I work on Windows 10, 64 bits.
I add package with pacman linux command on my mingw32 terminal (msys64 version of 2022/09/04).
I work on a Qt Creator editor and I have a .pro file to configure my Qt project.
I want to develop a C++ module for my application which use libp11 to get the Yubikey bin number and decrypt file.
Why I use a mingw32 terminal and not the mingw64, because, for the moment, the project is always in develop in QT 4.8.
What I do:
I read the README file and follow the installation step of the INSTALL file (first I follow the MinGW / MSYS chapter, then I follow the MSYS2 chapter.)
I do much more, but I don't remember every thing and I go in many wrong ways.
My problems and questions
I try to follow the examples find in GitHub.
I help me with the site cpp.hotexemples.com to find the second parameter for the PKCS11_CTX_load function. I find the st_engine_ctx structure on this project.
The project file:
TEMPLATE = app
CONFIG += console c++17
CONFIG -= app_bundle
CONFIG += qt
LIBS += \
-lp11 \
-lssl \
-lcrypto
SOURCES += \
TestYubikey.cpp \
main.cpp
HEADERS += \
TestYubikey.h
The header file:
#ifndef TESTYUBIKEY_H
#define TESTYUBIKEY_H
#include <libp11.h>
#include <cstdio>
#include <cstring>
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <openssl/objects.h>
#include <openssl/engine.h>
#include <openssl/ui.h>
/* Engine configuration */
/* The PIN used for login. Cache for the ctx_get_pin function.
* The memory for this PIN is always owned internally,
* and may be freed as necessary. Before freeing, the PIN
* must be whitened, to prevent security holes.
*/
struct st_engine_ctx
{
char *pin = nullptr;
size_t pin_length = 0;
int verbose = 0;
char *module = nullptr;
char *init_args = nullptr;
UI_METHOD *ui_method = nullptr;
void *callback_data = nullptr;
int force_login = 0;
/* Engine initialization mutex */
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
CRYPTO_RWLOCK *rwlock = nullptr;
#else
int rwlock;
#endif
/* Current operations */
PKCS11_CTX *pkcs11_ctx = nullptr;
PKCS11_SLOT *slot_list = nullptr;
unsigned int slot_count = 0;
};
class TestYubikey
{
public:
TestYubikey();
};
#endif // TESTYUBIKEY_H
The source file:
//libp11 is a wrapper library for PKCS#11 modules with OpenSSL interface
#include "TestYubikey.h"
#include <iostream>
TestYubikey::TestYubikey()
{
// Create a new libp11 context
PKCS11_CTX *ctx = PKCS11_CTX_new();
std::cout << "ctx = " << ctx << std::endl;
/* load pkcs #11 module */
int rc = PKCS11_CTX_load(ctx, "C:\\msys64\\mingw32\\lib\\engines-1_1\\pkcs11.dll"); //I test with "libpkcs11.dll" and "pkcs11.dll" too.
std::cout << "rc = " << rc << std::endl;
if (rc == -1)
{
std::cout << "Loading pkcs11 engine failed";
unsigned long error_code = ERR_get_error();
const char* error_detail = ERR_reason_error_string(error_code);
std::cout << " (" << error_code << ") : " << std::string(error_detail) << std::endl;
}
else
{
std::cout << "Loading pkcs11 engine worked !" << std::endl;
}
}
My output console show:
11:59:27: Starting C:/Users/jgomez/Documents/build-SandBox-Desktop_Qt_4_8_7_MinGW_32_bit-Release/release/SandBox.exe...
ctx = 0x2ca8f50
rc = -1
Loading pkcs11 engine failed (0) : terminate called after throwing an instance of 'std::logic_error'
what(): basic_string: construction from null is not valid
11:59:29: C:/Users/jgomez/Documents/build-SandBox-Desktop_Qt_4_8_7_MinGW_32_bit-Release/release/SandBox.exe exited with code 3
My problem:
rc = -1
Solution :
use the Dll called opensc-pkcs11.dll provided with OpenSC project and it should work.
( https://github.com/OpenSC/OpenSC , after installing , it should be found here by default C:\Program Files (x86)\OpenSC Project\OpenSC\pkcs11)
Explantation :
I have encountered the same problem, and after messing around with the files I figured out that this error due the PKCS11_CTX_Load function.
PKCS11_CTX_Load , tries to load the pkcs11.dll , and then tries getting the address of "c_get_function_list" from this dll, which fails since it doesn't have that function.
Windows 10, LLVM 7, GCC 8.1, Visual studio 2019.
#include <iostream>
#include <fstream>
using namespace std;
char exe[1000000] = {};
int n = 0;
int filesize;
void read() {
int pointer = 0;
cin >> filesize;
fstream f;
f.open("s.exe", ios::in | ios::app | ios::binary);
f.seekp(pointer, ios::beg);
while (pointer < filesize) {
f.read((char*)&n,sizeof(char));
exe[pointer] = n;
pointer += 1;
}
f.close();
}
void showMassive(){
int pointer = 0;
while(pointer<filesize){
cout << pointer << ":" << (unsigned int8_t)exe[pointer] << endl;
pointer+=1;
}
}
void showAssembler(){
}
void write() {
int pointer = 0;
fstream f;
f.open("s1.exe", ios::out | ios::app | ios::binary);
f.seekp(pointer, ios::beg);
while (pointer < filesize) {
n=exe[pointer];
pointer += 1;
f.write((char*)&n,sizeof(char));
}
f.close();
}
void MachineCodeOptimizer(){
//some code
exe[1031] += 1;//just for example
}
int main(){
read();
showMassive();
showAssembler();
MachineCodeOptimizer();
write();
return 0;
}
this code. Clang creates an exe file 312 kilobytes size at best (-O1 key). GCC creates 66 KB size exe anyway. What happens? Why so difference between compilers? I look at machine code, but dont understand. Now i tried visual studio 2019 - 26 KB! Visual studio 2019 showing result close to assembler(in file size).
Clang and GCC are two completely independent compilers. When you write code in your source language, you only specify what you want the machine to execute, not how it should do that. Compilers are free in choosing their ways to get there, as long as they stay within the limits that are specified by your source language. So it's not surprising that the two resulting executables differ in file size. Also the chosen instructions by the two compilers might differ a lot (or completely) since there are, for example, a dozen different ways to represent loops in machine code (incl. taking advantage of parallel execution of the target processor ... or not). You might want to check out Matt Godbolt's talk from 2017 (https://www.youtube.com/watch?v=bSkpMdDe4g4); this can give you a short but exhaustive introduction in what compilers actually do (for you) behind the scenes.
I am trying to perform run length compression on a string for a special protocol that I am using. Runs are considered efficient when the run size or a particular character in the string is >=3. Can someone help me to achieve this. I have live demo on coliru. I am pretty sure this is possible with the standard library's std::adjacent_find with a combination of std::not_equal_to<> as the binary predicate to search for run boundaries and probably using std::equal_to<> once I find a boundary. Here is what I have so far but I am having trouble with the results:
Given the following input text string containing runs or spaces and other characters (in this case runs of the letter 's':
"---thisssss---is-a---tesst--"
I am trying to convert the above text string into a vector containing elements that are either pure runs of > 2 characters or mixed characters. The results are almost correct but not quite and I cannot spot the error.
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
expected the following
======================
---,thi,sssss,---,is-a,---,tesst--,
actual results
==============
---,thi,sssss,---,is-a,---,te,ss,--,
EDIT: I fixed up the previous code to make this version closer to the final solution. Specifically I added explicit tests for the run size to be > 2 to be included. I seem to be having boundary case problems though - the all spaces case and the case where the end of the strings ends in several spaces:
#include <iterator>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
int main()
{
// I want to convert this string containing adjacent runs of characters
std::string testString("---thisssss---is-a---tesst--");
// to the following
std::vector<std::string> idealResults = {
"---", "thi", "sssss",
"---", "is-a",
"---", "tesst--"
};
std::vector<std::string> tokenizedStrings;
auto adjIter = testString.begin();
auto lastIter = adjIter;
// temporary string used to accumulate characters that
// are not part of a run.
std::unique_ptr<std::string> stringWithoutRun;
while ((adjIter = std::adjacent_find(
adjIter, testString.end(), std::not_equal_to<>())) !=
testString.end()) {
auto next = std::string(lastIter, adjIter + 1);
// append to foo if < run threshold
if (next.length() < 2) {
if (!stringWithoutRun) {
stringWithoutRun = std::make_unique<std::string>();
}
*stringWithoutRun += next;
} else {
// if we have encountered non run characters, save them first
if (stringWithoutRun) {
tokenizedStrings.push_back(*stringWithoutRun);
stringWithoutRun.reset();
}
tokenizedStrings.push_back(next);
}
lastIter = adjIter + 1;
adjIter = adjIter + 1;
}
tokenizedStrings.push_back(std::string(lastIter, adjIter));
std::cout << "expected the following" << std::endl;
std::cout << "======================" << std::endl;
std::copy(idealResults.begin(), idealResults.end(), std::ostream_iterator<std::string>(std::cout, ","));
std::cout << std::endl;
std::cout << "actual results" << std::endl;
std::cout << "==============" << std::endl;
std::copy(tokenizedStrings.begin(), tokenizedStrings.end(), std::ostream_iterator<std::string>(std::cout, ","));
std::cout << std::endl;
}
if (next.length() < 2) {
if (!stringWithoutRun) {
stringWithoutRun = std::make_unique<std::string>();
}
*stringWithoutRun += next;
}
This should be if (next.length() <= 2). You need to add a run of identical characters to the current token if its length is either 1 or 2.
I seem to be having boundary case problems though - the all spaces
case and the case where the end of the strings ends in several spaces
When stringWithoutRun is not empty after the loop finishes, the characters accumulated in it are not added to the array of tokens. You can fix it like this:
// The loop has finished
if (stringWithoutRun)
tokenizedStrings.push_back(*stringWithoutRun);
tokenizedStrings.push_back(std::string(lastIter, adjIter));
i have written this small piece of Code for testing purposes:
#include <iostream>
#include "SDL2/SDL.h"
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
// Betriebssystem ermitteln
std::string PlatFormString;
PlatFormString = SDL_GetPlatform();
std::cout << PlatFormString << "\n";
// Separator ermitteln
char Separator = '/';
if (PlatFormString == "Windows") {
Separator = '\\';
}
std::cout << "Separator: " << Separator << "\n";
// Installationspfad ermitteln
std::string InstallPath;
InstallPath = SDL_GetBasePath();
std::cout << InstallPath << "\n";
// Benutzerverzeichnis ermitteln
char* UserPath;
UserPath = SDL_GetPrefPath("TFF", "Blaster");
if (UserPath == nullptr) {
std::cout << "No Userpath aviable !! \n";
}
else {
std::cout << UserPath << "\n";
}
SDL_Quit();
return 0;
};
Under Linux eerthing works fine.
But under Windows, i am getting these strange errors ...
-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------
g++.exe -LD:\mingw64 -LD:\mingw64\bin -LD:\mingw64\include -LD:\mingw64\include\SDL2 -LD:\mingw64\lib -o bin\Debug\Test.exe obj\Debug\src\Test.o -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer ..\..\mingw64\lib\libSDL2main.a ..\..\mingw64\lib\libSDL2.a
..\..\mingw64\lib\libSDL2.a(SDL_systimer.o): In function `timeSetPeriod':
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x64/../src/timer/windows/SDL_systimer.c:58: undefined reference to `__imp_timeBeginPeriod'
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x64/../src/timer/windows/SDL_systimer.c:52: undefined reference to `__imp_timeEndPeriod'
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x64/../src/timer/windows/SDL_systimer.c:58: undefined reference to `__imp_timeBeginPeriod'
and so on. I dont know whats going on there. Can anyone help ?
I#m using Codeblocks 13.12, minGW64 (4.8.1), SDL 2.0.3 and Windows 7 64bit
You need to link against winmm.lib.
Try adding
#pragma comment(lib, "winmm.lib")
to your source.
I am posting this about a year later but for the future searchers here is the solution. Replace libSDL2.a with libSDL2.dll.a and it will compile just fine. The issue has something to do with dynamic and static linking with a windows machine or something I personally do I understand it completely but it works.
I came across the solution by reading this article: http://tech.yipp.ca/sdl/how-to-fix-libsdla-undefined-reference/
However this goes on a whole other solution I read between the lines or more particularly.
This is a really a rare problem that would occur only when you try to link with libSDL.a static library instead of the dynamic library SDL.dll. Then you have to add those library that SDL.dll normally links against which are the three above.
I am using the boost library for getting the current system time and my code works but visualt studio 2010 exits after the program.the debugger breaks while trying to free the non existing pointer. I know this is because of the boost native code.Since there is no error if I comment the boost portion of code.
Till now I tried using the #pragma as explained in MSDN but with no success.Can someone provide me some suggestions.? (I also tried GetSystemTime function to get the time but i cannot get the microsecond detail like boost.)
MY Code
#pragma managed(push, off)
void GetSystemDateTime(SDateTime& stimeblock);
#pragma managed(pop)
int main()
{
c++/cli code
SDateTime stimestruct[1];
//call to the function having the boost code..
GetSystemDateTime(stimestruct[0]);
}
Function Definition
#pragma managed(push, off)
void GetSystemDateTime(SDateTime& timeblock)
{
// SYSTEMTIME time;
// GetSystemTime(&time);
// WORD millis = (time.wSecond * 1000) + time.wMilliseconds;
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
std::tm pt_tm = to_tm(now);
std::cout << now << std::endl;
//std::cout << time.wYear<< time.wMonth<<time.wDay //<<time.wHour<<time.wMinute<<time.wSecond<<time.wMilliseconds << std::endl;
std::string timestring = to_iso_string(now);
std::string sYear = timestring.substr (0,4);
std::string sMonth = timestring.substr (4,2);
std::string sDay = timestring.substr (6,2);
std::string sHour = timestring.substr (9,2);
std::string sMinute = timestring.substr (11,2);
std::string sSecond = timestring.substr (13,2);
std::string sUSecond = timestring.substr (16);
istringstream isYear(sYear);
istringstream isMonth(sMonth);
istringstream isDay(sDay);
istringstream isHour(sHour);
istringstream isMinute(sMinute);
istringstream isSec(sSecond);
istringstream isUSec(sUSecond);
// use is like an input stream
int iYear,iMonth,iDay,iHour,iMinute,iSecond,iUSecond;
isYear >> iYear;
isMonth >>iMonth;
isDay >>iDay;
isHour >>iHour;
isMinute >>iMinute;
isSec >>iSecond;
isUSec >>iUSecond;
timeblock.uiYear = iYear;
timeblock.usiMonth = time.wMonth;
timeblock.usiDay = time.wDay;
timeblock.usiHour = time.wHour;
timeblock.usiMinute = time.wMinute;
timeblock.usiSec = time.wSecond;
timeblock.udiUSec = time.wMilliseconds;
// Display version information
}
I've seen this error caused by using a static variable in native code in a C++/CLI assembly.
The only workaround I found was to remove the static variable, e.g., by moving it to class or file scope.
However, if this static variable is in the boost code, doing so may not be easy/possible. In that case, you could create a separate C++ file that's compiled without /clr, use the boost function in that file, and link that into your C++/CLI assembly.
This error seems to be caused by the compiler generating incorrect code. I filed a bug with Microsoft, which was closed "won't fix", but the compiler team gave some other workarounds in their response.
Try using
#pragma managed(push, off)
#pragma managed(pop)
around the #include lines for all boost header files.
I'm on the same problem for a few days now.
this is the best workaround i have found. and also explains why this is happening.
look at the end (number 7 and 9)
hope this helps http://www.codeproject.com/Articles/442784/Best-gotchas-of-Cplusplus-CLI