What is the C++ equivalent of this python code? - python

li = list(map(int,input().split()))
I am pretty new to c++ . I essentialy want the easiest verison of this code which takes in the input I pass in via terminal and push back the output to a vector.
I've tried:
#include <iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string input;
vector<int> numbers;
while(getline(cin,input,' ')){
numbers.push_back(stoi(input));
}
for(int i : numbers){
cout << i << endl;
}
return 0;
}
I am using g++ 9.2.0.
The same code works fine on an online ide. I am not sure if it's an issue with the g++ compiler or not.
Weird Stuff!

Your example runs fine for me: https://ideone.com/bFLjB1
You could clean this up a bit by using the default type deduction and whitespace-splitting behavior of cin's operator>>:
std::vector<int> numbers;
int temp = 0;
while (std::cin >> temp) {
numbers.push_back(temp);
}

You can construct a container directly from whitespace delimited input.
#include <iostream>
#include <vector>
#include <iterator>
int main() {
std::vector<int> numbers(std::istream_iterator<int>(std::cin), {});
for(int i : numbers){
std::cout << i << std::endl;
}
return 0;
}

Related

Calling parallel C++ code in Python using Pybind11

I have a C++ code that runs in parallel with OpenMP, performing some long calculations. This part works great.
Now, I'm using Python to make a GUI around this code. So, I'd like to call my C++ code inside my python program. For that, I use Pybind11 (but I guess I could use something else if needed).
The problem is that when called from Python, my C++ code runs in serial with only one thread/CPU.
I tried (in two ways) to understand what is done in the documentation of pybind11 here but it does not seem to work at all.
My binding looks like that :
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "../cpp/include/myHeader.hpp"
namespace py = pybind11;
PYBIND11_MODULE(my_module, m) {
m.def("testFunction", &testFunction, py::call_guard<py::gil_scoped_release>());
m.def("testFunction2", [](inputType input) -> outputType {
/* Release GIL before calling into (potentially long-running) C++ code */
py::gil_scoped_release release;
outputType output = testFunction(input);
py::gil_scoped_acquire acquire;
return output;
});
}
Problem: This still does not work and uses only one thread (I verify that with a print of omp_get_num_threads() in an omp parallel region).
Question: What am I doing wrong? What do I need to do to be able to use parallel C++ code inside Python?
Disclaimer: I must admit I don't really understand the GIL thing, particularly in my case where I do not use Python inside my C++ code, which is really "independent" in theory. I just want to be able to use it in another (Python) code.
Have a great day.
EDIT :
I have solved my problem thanks to the pptaszni's answer. Indeed, the GIL things are not needed at all, I misunderstood the documentation. pptaszni's code worked and in fact it was a problem with my CMake file.
Thank you.
It's not really a good answer (too long for a comment thought), because I did not reproduce your problem, but maybe you can isolate the issue in your code by trying this example that works for me:
C++ code:
#include "OpenMpExample.hpp"
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
#include <omp.h>
constexpr int DATA_SIZE = 10000000;
std::vector<int> testFunction()
{
int nthreads = 0, tid = 0;
std::vector<std::vector<int> > data;
std::vector<int> results;
std::random_device rnd_device;
std::mt19937 mersenne_engine {rnd_device()};
std::uniform_int_distribution<int> dist {-10, 10};
auto gen = [&dist, &mersenne_engine](){ return dist(mersenne_engine); };
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
if (tid == 0)
{
nthreads = omp_get_num_threads();
std::cout << "Num threads: " << nthreads << std::endl;
data.resize(nthreads);
results.resize(nthreads);
}
}
#pragma omp parallel private(tid) shared(data, gen)
{
tid = omp_get_thread_num();
data[tid].resize(DATA_SIZE);
std::generate(data[tid].begin(), data[tid].end(), gen);
}
#pragma omp parallel private(tid) shared(data, results)
{
tid = omp_get_thread_num();
results[tid] = std::accumulate(data[tid].begin(), data[tid].end(), 0);
}
for (auto r : results)
{
std::cout << r << ", ";
}
std::cout << std::endl;
return results;
}
I tried to keep the code short, but force the machine to actually do some computations at the same time. Each thread generates 10^7 random integers and then sums them up. Then the python binding does not even require gil_scoped_release:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "OpenMpExample.hpp"
namespace py = pybind11;
// both versions work for me
// PYBIND11_MODULE(mylib, m) {
// m.def("testFunction", &testFunction, py::call_guard<py::gil_scoped_release>());
// }
PYBIND11_MODULE(mylib, m) {
m.def("testFunction", &testFunction);
}
Example output from python:
Python 3.6.8 (default, Jun 29 2020, 16:38:14)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mylib
>>> x = mylib.testFunction()
Num threads: 12
-10975, -22101, -11333, -28603, -471, -15505, -18141, 2887, -6813, -5328, -13975, -4321,
My environment: Ubuntu 18.04.3 LTS, gcc 8.4.0, openMP 201511, python 3.6.8;

Windows application using python and c++

I want to start working on my own windows software. I know python and I am learning c++. How can I create windows software with the frontend written in c++ and the backend in python?
I know this is possible because blender is written in c++ and python aswell
You can embed Python into C++.
Documentation
The simplest form of embedding Python is the use of the very high level interface. This interface is intended to execute a Python script without needing to interact with the application directly. This can for example be used to perform some operation on a file.
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
return 0;
}

Interactive session with another application in child process in Python [duplicate]

This question already has answers here:
Interactive input/output using Python
(4 answers)
Closed 5 years ago.
I have an application(.exe) written in any language eg. C++ and want to run the application from python. I can run that simple application by using below sample Python code by following tutorial here https://docs.python.org/2/library/subprocess.html
from subprocess import Popen, PIPE
process = Popen([r"C:\Users\...\x64\Debug\Project13.exe"],stdout = PIPE,
stderr=PIPE, stdin = PIPE)
stdout = process.communicate(input = b"Bob")[0]
print(stdout)
C++ code:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
void foo(string s) {
for (int i = 0; i < 3; ++i)
{
cout << "Welcome " << s << " ";
Sleep(2000);
}
}
int main()
{
string s;
cin>> s;
foo(s);
return 0;
}
This works fine for me. But what if i am reading input in C++ application multiple times as below:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
string s;
for (int i = 0; i < 3; ++i)
{
cin >> s;
cout << "Welcome " << s << " ";
Sleep(2000);
}
return 0;
}
Here i am not able to use process.communicate() multiple times since the child has already exited by the time it returns.Basically i want to interact with the programme as a continuous session.
I want suggestion or any other approach to solve this issue in python? Thanks in advance.
Assuming you are working on windows I would suggest having a look at namedpipes, on python end you can use PyWPipe, on c++ end you will have to write your own wrappers to get the message for the process.

Enter some parameter to program with python

If I have source code like this
#include <stdio.h> //test
int main(void)
{
int tmp;
scanf("%d", &tmp);
printf("%d\n", tmp);
}
I know I can give parameter with python like (python -c 'print "1234"';cat) | ./test
But I have problem with other case. For example if program get integer with scanf and string with read.
#include <stdio.h> //test
#include <unistd.h>
int main(void)
{
int tmp1=0;
char tmp2[100]={0};
scanf("%d", &tmp1);
read(0, tmp2, 100);
printf("%d %s\n", tmp1, tmp2);
}
I tried like this (python -c 'print "134\n"+"Hello World\n"';cat) | ./test I think result may be 134 Hello World However result was just 134. I can't input string with this method.
I can't find other way to solve this problem. Is there any method to solve this problem?
I'm using x64 Ubuntu 16.04 LTS and compile option was -o test -m32 test.c

Access violation when embedding Python on Windows

I need help understanding why embedded Python is crashing in this extremely simple test case on Windows.
This works great:
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#endif
#include <iostream>
int main()
{
Py_Initialize();
std::cout << "Hello world!" << std::endl;
PyRun_SimpleString("print(\"Hello world!\")");
return 0;
}
This crashes with an access violation:
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#endif
#include <iostream>
int main()
{
Py_Initialize();
std::cout << "Hello world!" << std::endl;
std::cout << Py_GetPythonHome() << std::endl;
return 0;
}
My research has led me here. I determined that my python installation (2.6.5) is compiled to use msvcr90.dll and the embedding program uses msvcr100.dll.
The first thing that grabbed my attention was that Py_GetPythonHome() surely checks environment variables, one of the flagged problems of having multiple MS C runtimes used in a single application. However according to the MSDN example, I should only expect the environment variable values to be out of synch, not to cause an access violation.
Please help me understand!

Categories