In title, I am specific about the task that I want to achieve. I want to utilize the c++17 features such as parallel STL etc. On visual studio 2017, I configure to c++17 under project properties for language. Even after doing this I get the error with #include that no execution file.
I am just starting with simple example of array addition in parallel with C++ 17 algorithms. How do I resolve this?
Source:
#include <stddef.h>
#include <stdio.h>
#include <algorithm>
#include <execution>
#include <chrono>
#include <random>
#include <ratio>
#include <vector>
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::milli;
using std::random_device;
using std::sort;
using std::vector;
const size_t testSize = 1'000'000;
const int iterationCount = 5;
void print_results(const char *const tag, const vector<double>& sorted,
high_resolution_clock::time_point startTime,
high_resolution_clock::time_point endTime) {
printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(),
sorted.back(),
duration_cast<duration<double, milli>>(endTime - startTime).count());
}
int main() {
random_device rd;
// generate some random doubles:
printf("Testing with %zu doubles...\n", testSize);
vector<double> doubles(testSize);
for (auto& d : doubles) {
d = static_cast<double>(rd());
}
// time how long it takes to sort them:
for (int i = 0; i < iterationCount; ++i)
{
vector<double> sorted(doubles);
const auto startTime = high_resolution_clock::now();
sort(sorted.begin(), sorted.end());
const auto endTime = high_resolution_clock::now();
print_results("Serial", sorted, startTime, endTime);
}
}
and this is error:
Error C1083 Cannot open include file: 'execution': No such file or directory
Task that I want to achieve is that C++17 with CUDA GPU. Both new to me although not c++ in itself. But I am interested in parallel STL of C++17 with CUDA. I want to start from base. Any suggestions will help me?
Thanks,
Govind
Please check if the header file is included in the header file directory. the C++ headers path are:
1.C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include
2.C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt
The first contains standard C++ headers such as iostream. The second contains legacy C headers such as stdio.h.
If you are going to use C++ to develop desktop applications, I recommend you to refer to my setup.
Also I tested your code on VS2022 without any errors. So I suggest you to use a higher version of VS and install the environment you need.
Related
I was trying to use shared pointers in CUDA by using NVIDIA's version of the C++ standard library. So tried to include <cuda/std/detail/libcxx/include/memory>. I got some errors. One of them includes cannot open source file with <__config> and <__functional_base>. Those files were clearly in the directory. It's like visual studios acts like those files don't exist even though they do. Another error I get is linkage specification is incompatible with previous "" with <cmath>.
I did little digging. I found out that cannot open source file is apparent with every non-header file that starts with _ in cuda/std/detail/libcxx/include/. It is like Visual Studio somehow acts like those files don't exist despite being clearly located in the additional include directories. Furthermore, when I type cuda/std/detail/libcxx/include/, IntelliSense won't find these files. If I can get visual studio to recognize those files, I can properly include any files in NVIDIA's version of standard library.
The first thing to understand is that CUDA doesn't have a C++ standard library. What you are referring to is the libcu++, which is an extremely bare bones heterogenous reimplementation of a tiny subset of what is defined in the C++ standard library. You can use whatever is defined in libcu++ (and that is not much, it is a very incomplete implementation) as follows:
Prepend the local path cuda/std/ to whatever standard library header you are using to substitute the import from the native host C++ standard library to libcu++
Change the namespace from std to cuda::std
compile using nvcc
As a simple example:
$ cat atomics.cpp
#include <iostream> // std::cout
#include <cuda/std/atomic> // cuda::std::atomic, cuda::std::atomic_flag, ATOMIC_FLAG_INIT
#include <thread> // std::thread, std::this_thread::yield
#include <vector> // std::vector
cuda::std::atomic<bool> ready (false);
cuda::std::atomic_flag winner = ATOMIC_FLAG_INIT;
void count1m (int id) {
while (!ready) { std::this_thread::yield(); } // wait for the ready signal
for (volatile int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.test_and_set()) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
$ nvcc -std=c++11 -o atomics atomics.cpp -lpthread
$ ./atomics
spawning 10 threads that count to 1 million...
thread #6 won!
Note that as per the documentation, there are presently (CUDA 11.2) only implementations of:
<atomic>
<latch>
<barrier>
<semaphore>
<chrono>
<cfloat>
<ratio>
<climits>
<cstdint>
<type_traits>
<tuple>
<functional>
<utility>
<version>
<cassert>
<cstddef>
with complex support coming in the next CUDA release from the looks of things.
You mentioned shared pointers. There is no <memory> implementation at present, so that cannot be made to work.
When I compiled this program (from C++ Programming Language 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h only for the _TCHAR macro.
Then, precompiled header is usually a per-project file in Visual Studio world, so:
Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
Change the #include <stdafx.h> to #include "stdafx.h". It is supposed to be a project local file, not to be resolved in include directories.
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h.
Just include windows.h instead of stdfax or create a clean project without template.
There are two solutions for it.
Solution number one:
1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)
Solution Number two:
1.Create stdafx.h and stdafx.cpp in your project
2 Right click on project -> properties -> C/C++ -> Precompiled Headers
3.select precompiled header to create(/Yc)
4.Rebuild the solution
Drop me a message if you encounter any issue.
You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.
Add #include "afxwin.h" in your source file. It will solve your issue.
Just running through a Visual Studio Code tutorial and came across a similiar issue.
Replace #include "stdafx.h" with #include "pch.h" which is the updated name for the precompiled headers.
I've been using Visual Studio Code Runner to compile C++ projects without a problem, but now I need to use the GMP backend with Boost Multiprecision Library.
As I showed in a different question, I already have working code that relies only on Boost Multiprecision Library. What I'm trying to do is to use GMP's high precision floats instead of the ones that come with Boost. Like the following:
#include <iostream>
#include <boost/math/distributions/negative_binomial.hpp>
#include <boost/multiprecision/gmp.hpp>
using namespace std;
using namespace boost;
typedef boost::multiprecision::number<boost::multiprecision::gmp_float<50>> myPrecision;
// typedef boost::multiprecision::mpf_float_50 myPrecision; // this also raises the errors
template <class T1, class T2>
myPrecision negbinPDF(T1 passed_val, T2 passed_par1, T2 passed_par2)
{
myPrecision val = myPrecision(passed_val);
myPrecision par1 = myPrecision(passed_par1);
myPrecision par2 = myPrecision(passed_par2);
return math::pdf(math::negative_binomial_distribution<myPrecision>(par1, par2), val);
};
int main() {
auto p = negbinPDF(1.23456789012345678901234567890, 8.0, 0.25);
cout << "The PDF is: " << p << endl;
}
When running the above code, many errors are raised (see here), starting with some undefined references. Hence my question: How can I link to GMP using Visual Studio Code's Code Runner (in Linux)?
Whenever I use standard library containers stack, queue, deque or priority_queue, performance in Visual Studio becomes inexplicably slow. The same program that can run in gcc compiler (Qt Creator) within few seconds takes over a minute in Visual Studio.
Here is a simple program that uses BFS to check if a number can be transformed into a target number. Allowed transformations are x->x+1 and x->x/2.
Code:
#include <queue>
#include <stack>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <chrono>
using namespace std;
using namespace std::chrono;
const int M=10000000;
int vis[M+1];
bool can(int s, int t) {
memset(vis, 0, sizeof(vis));
stack<int> q;
q.push(s);
int m=0;
vis[s]=true;
while(!q.empty()) {
m=max(m, (int)q.size());
int top=q.top();
q.pop();
if(top==t) return true;
if(top+1<=M && !vis[top+1]) {
q.push(top+1);
vis[top+1]=true;
}
if(!vis[top/2]) {
q.push(top/2);
vis[top/2]=true;
}
}
return false;
}
int main() {
vector <int> S {8769154, 9843630, 2222222, 1, 3456789};
vector<int> T {94383481, 1010101, 9999999, 9876543, 1};
high_resolution_clock::time_point t1=high_resolution_clock::now();
for(int i=0; i<S.size(); i++) {
cout<<can(S[i], T[i])<<endl;
}
high_resolution_clock::time_point t2=high_resolution_clock::now();
auto duration=std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
cout<<"Execution time "<<duration/1000000.0<<" second";
return 0;
}
Output:
Visual Studio : Execution time 77.3784 second
Qt Creator : Execution time 4.79727 second
Output of The same program on using stack instead of queue:
Visual Studio : Execution time 114.896 second
Qt Creator : Execution time 4.35225 second
So Qt Creator runs almost 20-30 times faster than Visual Studio in this case. I have no idea why this happens. The performance difference is very little when I Don't use these STL containers.
As noted in the comments, Visual Studio is slow in debug mode. It's partially because optimizations are off, partially because the Standard Library implementation in VC++ has a lot of checks on iterator abuse.
Running on Widows 7 with MSVS 2010
I am following this tutorial to understand how to use MPIR library for adding two big integers
I understand this library should help me in adding very big numbers as shown in the program below:
#include < stdio.h>
#include < stdlib.h>
#include < gmpxx.h>
#include < iostream>
using namespace std;
void main(int argc, char *argv[])
{
mpz_class answer_a = 111111111111111111111111111111111111111111111111;
mpz_class answer_b = 111111111111111111111111111111111111111111111111;
mpz_class answer_c;
answer_c= answer_b + answer_a ;
cout << answer_c<<"\n";
}
But still I get error C2177: constant too big.
Did I misunderstand MPIR ?
Such constant is (very likely) too big for standard integer types. You should use char * constructor instead:
void mpz_class::mpz_class (const char *s)
For example:
mpz_class answer_a("111111111111111111111111111111111111111111111111");
to make this work you need to include suitable MPIR C++ interface header (notice that <gmpxx.h> is from C++ interface of GNU MP library):
#include <mpirxx.h>
See 12.2 C++ Interface Integers chapter in MPIR documentation for more details.