假设我有10*.hpp和*.cpp文件,我需要编译代码.我知道我将需要许多不同代码的相同文件.我可以使用那些允许我简单编写的文件创建一个"包"
#include<mypackage>
Run Code Online (Sandbox Code Playgroud)
代替
#include"file1.hpp"
#include"file2.hpp"
...
#include"file10.hpp"
Run Code Online (Sandbox Code Playgroud)
每次我需要这个"包"时,我都不需要编写一个makefile.
更确切地说,我使用linux.
第一个问题,如果我将在接下来的3年中开发代码,那么开始使用c ++ 11是一件好事吗?
如果是的话,如果我想将它与Lapack一起使用,那么实现矩阵的"最佳"方法是什么?我的意思是,做std::vector<std::vector< Type > > Matrix不容易与Lapack兼容.
到目前为止,我存储了我的矩阵Type* Matrix(new Type[N])(指针形式带有new并且 delete很重要,因为数组的大小不是像5那样的数字,而是作为变量).
但是使用C++ 11可以使用std :: array.根据这个网站,这个容器似乎是最好的解决方案......你怎么看?
想象一下我有一个包含两列的数据文件。gnuplot跑步中
stats 'datafile' u 1:2
Run Code Online (Sandbox Code Playgroud)
允许我找到两列的最小值和最大值。变量
STATS_index_min_x和STATS_index_min_y分别给出第一列和第二列的最小值的索引。它在文档中说它们是这样的
data[STATS_index_min_x] == STATS_min_x
Run Code Online (Sandbox Code Playgroud)
现在想象一下需要访问data[STATS_index_min_x-1]or
data[STATS_index_min_x+1]。我怎样才能做到这一点 ?实际上,我如何访问中任何列的任何特定数据gnuplot?
我怎样才能实现正确的排序?
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
inline bool sort_string_swiss(std::string const& a, std::string const& b){
return std::use_facet<std::collate<char> >(std::locale("fr_CH.UTF-8")).compare(&a[0], &a[0] + a.size(), &b[0], &b[0] + b.size())<0;
}
int main(){
std::vector<std::string> list{"De rien", "Décider","Devant"};
std::cout<<"Correct: ";
for(auto const& l:list){ std::cout<<l<<" "; } //outputs De rien Décider Devant
std::sort(list.begin(),list.end(),sort_string_swiss);
std::cout<<std::endl<<"Not correct: ";
for(auto const& l:list){ std::cout<<l<<" "; } //outputs Décider De rien Devant
std::sort(list.begin(),list.end());
std::cout<<std::endl<<"Not correct: ";
for(auto const& l:list){ std::cout<<l<<" "; } //outputs De rien Devant Décider
std::cout<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
上面给出的代码没有给出正确的输出.我希望通过正确处理的特殊字符实现字典排序(如列表初始化时所给出的).我查看了不同的帖子,但似乎都没有符合我的要求.我查看 …
这是困扰我的最小例子
#include <iostream>
#include <memory>
#include"omp.h"
class A{
public:
A(){std::cout<<this<<std::endl;}
};
int main(){
#pragma omp parallel for
for(unsigned int i=0;i<4;i++){
std::shared_ptr<A> sim(std::make_shared<A>());
}
for(unsigned int i=0;i<4;i++){
std::shared_ptr<A> sim(std::make_shared<A>());
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行该代码几次,我可能得到这样的结果:
0xea3308
0xea32d8
0xea3338
0x7f39f80008c8
0xea3338
0xea3338
0xea3338
0xea3338
Run Code Online (Sandbox Code Playgroud)
我意识到最后4个输出总是具有相同的字符数(8).但由于某种原因,它发生(并非总是)四个第一个输出中的一个或多个包含更多(14)个字符.看起来使用openmp改变了指针的"本质"(这是我天真的理解).但这种行为是正常的吗?我应该期待一些奇怪的行为吗?
这是一个实时测试,在稍微复杂的代码版本中显示相同的问题
假设我有一个方法乘以两个std::vector:
double multiply(std::vector<double> const& a, std::vector<double> const& b){
double tmp(0);
/*here I could easily do a parallelization with*/
/*#pragma omp parallel loop for*/
for(unsigned int i=0;i<a.size();i++){
tmp += a[i]*b[i];
}
return tmp;
}
Run Code Online (Sandbox Code Playgroud)
如果我在这个函数中设置了pragma宏,那么调用multiply(...)将在所有线程上运行.
现在假设我还想做其他一些向量乘法:
void many_multiplication(std::vector<double>* a, std::vector<double>* b, unsigned int N){
/*here I could easily do a parallelization with*/
/*#pragma omp parallel loop for*/
for(unsigned int i=0;i<N;i++){
for(unsigned int j=0;j<N;j++){
multiply(a[i],b[j]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也可以用同样的方式进行并行化.但这会导致不必要的嵌套并行性.
如何检查是否multiply(..)在并行区域内调用,然后pragma宏multiply(...)"关闭".如果从非平行区域调用它,那么它就会"开启".
我有一个FILE* file保存一些二进制数据的。假设这个数据是一个双精度列表,最后一个条目是一个描述双精度的字符串。我想修改这个字符串(新字符串可能更短)。所以首先我删除旧字符串。我需要找到字符串的起点:
fseek(file,-size(sring.size()),SEEK_END);
Run Code Online (Sandbox Code Playgroud)
然后我该怎么办?我找到了Delete End of File链接,但我不知道该使用哪一个...重新调整文件大小后,我可以简单地使用 编写我的新字符串fwrite吗?
我的问题是这样的一个.但我想做点不同的事......
例如,在我的并行区域内,我想在4个线程上运行我的代码.当每个线程进入for循环时,我想在8个线程上运行我的代码.就像是
#pramga omp parallel num_threads(4)
{
//do something on 4 threads
#pragma omp parallel for num_threads(2)
for(int i=0;i<2;i++){
//do something on 8 threads in total
}
}
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法将每个(4)运行线程"拆分"为两个(新)线程,因此在for循环中有更多(8)个线程正在运行?
我尝试了两件事:
class RandDouble{
public:
RandDouble(double const& min_inclusive, double const& max_exclusive):
mt_(std::random_device()),
dist_(min_inclusive,max_exclusive)
{}
~RandDouble(){}
double get(){ return dist_(mt_); }
private:
std::mt19937_64 mt_;
std::uniform_real_distribution<double> dist_;
};
class RandUnsignedInt{
public:
RandUnsignedInt(unsigned int const& min_inclusive, unsigned int const& max_inclusive):
mt_(std::random_device()),
dist_(min_inclusive,max_exclusive)
{}
~RandUnsignedInt(){}
unsigned int get(){ return dist_(mt_); }
private:
std::mt19937_64 mt_;
std::uniform_int_distribution<unsigned int> dist_;
};
Run Code Online (Sandbox Code Playgroud)
和
template<typename Type>
class Rand{
public:
Rand(Type const& min_inclusive, Type const& max_exclusive);/
~Rand();
Type get();
private:
std::mt19937_64 mt_;
std::uniform_real_distribution<double>* dist_double_;
std::uniform_int_distribution<unsigned int>* dist_u_int_;
};
template<typename Type>
Rand<Type>::~Rand(){ …Run Code Online (Sandbox Code Playgroud)