我正在用c#编写一个软件,它需要多次调用,并且需要多个线程调用c ++非托管dll中的函数.
我有一个这样的C++文件:
// "variables" which consist in some simple variables (int, double)
// and in some complex variables (structs containing arrays of structs)
extern "C"
{
__declspec(dllexport) int function1()
{
// some work depending on random and on the "variables"
}
}
Run Code Online (Sandbox Code Playgroud)
和那样的C#类
public class class1
{
// "variables" <--- the "same" as the C++ file's ones
// Dll import <--- ok
public void method1()
{
int [] result;
for(int i=0; i<many_times; i++)
{
result = new int[number_of_parallel_tasks];
Parallel.For(0, …Run Code Online (Sandbox Code Playgroud) 我有一个可以并行化的C++程序.我正在使用Visual Studio 2010,32位编译.
简而言之,该计划的结构如下
#define num_iterations 64 //some number
struct result
{
//some stuff
}
result best_result=initial_bad_result;
for(i=0; i<many_times; i++)
{
result *results[num_iterations];
for(j=0; j<num_iterations; j++)
{
some_computations(results+j);
}
// update best_result;
}
Run Code Online (Sandbox Code Playgroud)
由于每个some_computations()都是独立的(读取了一些全局变量,但没有修改全局变量),我并行化了内部for循环.
我的第一次尝试是使用boost :: thread,
thread_group group;
for(j=0; j<num_iterations; j++)
{
group.create_thread(boost::bind(&some_computation, this, result+j));
}
group.join_all();
Run Code Online (Sandbox Code Playgroud)
结果很好,但我决定尝试更多.
我试过OpenMP库
#pragma omp parallel for
for(j=0; j<num_iterations; j++)
{
some_computations(results+j);
}
Run Code Online (Sandbox Code Playgroud)
结果比boost::thread那些更差.
然后我尝试了ppl库并使用parallel_for():
Concurrency::parallel_for(0,num_iterations, [=](int j) { …Run Code Online (Sandbox Code Playgroud) 我有这个代码
myvector <- c(3.45235, 1.32525, ... , 2.41351) # some numbers
write(myvector, "C:/mypath/myfile.txt") # I use "/" instead of "\"
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
文件错误(文件,ifelse(追加,"a","w")):无法打开连接另外:警告消息:在文件中(文件,ifelse(追加,"a","w")):不能打开文件'C:/mathath/myfile.txt':没有这样的文件或目录
我读了这个教程,但我无法理解我的代码有什么问题.任何的想法?
编辑:
正如@dickoa所指出的,我需要一个现有的路径来写一个文件,所以我试着用以下方式简化:
file.exists("C:/")
write(myvector, "C:/myfile.txt")
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是:P路径"C:/"存在(结果为TRUE)但我收到类似的错误:
文件错误(文件,ifelse(追加,"a","w")):无法打开连接另外:警告消息:在文件中(文件,ifelse(追加,"a","w")):不能打开文件'C:/mathath/myfile.txt':权限被拒绝
阅读Wikibook Optimizing C++,在本段中有以下建议:
如果整数值在应用程序代码中是常量,但在库代码中是变量,则将其设为模板参数.
所以,如果我有一个像这样的功能
void myfunction(int param)
{
switch(param)
{
case 1:
do_something_1();
break;
case 2:
do_something_2();
break;
...
case 100: // 100 is taken as example
do_something_100();
break;
}
}
Run Code Online (Sandbox Code Playgroud)
是否方便用以下更换?
template<int param> void myfunction()
{
switch(param)
{
case 1:
do_something_1();
break;
case 2:
do_something_2();
break;
...
case 100: // 100 is taken as example
do_something_100();
break;
}
}
Run Code Online (Sandbox Code Playgroud)
还是完全没必要?你能告诉我原因吗?
我正在使用Visual Studio 2012.
我曾多次使用预处理程序指令
#ifdef something
#include<some_header.h>
#else
#include<other_header.h>
#endif
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以以类似的方式链接静态库:
#ifdef something
// use some_library.lib
#else
// use other_library.lib
#endif
Run Code Online (Sandbox Code Playgroud)
这个问题来自我上一个问题的主题:我有两个静态库lib1.lib,lib2.lib(不是它们的代码)没有名称空间,具有相同的函数原型,但具有不同的实现.
我有一个代码如下:
int n;
int get_the_number();
void some_computations();
int main()
{
n = get_the_number();
some_computations()
return(0);
}
Run Code Online (Sandbox Code Playgroud)
该get_the_number功能得到一些输入,并返回整数n,其中它的呼叫后,将不会被修改.
在该some_computation函数中有以下代码
std::vector<my_struct> my_array;
for(int i=0; i<n; i++)
{
my_struct struct_temp;
// fill struct_temp;
my_array.push_back(struct_temp);
}
Run Code Online (Sandbox Code Playgroud)问题:既然my_array已知先验的大小,是否可以std::vector用std::array?替换?而且,在肯定的情况下,我应该期望在效率方面获得收益吗?
我试图用.替换矢量声明
std::array<my_struct,n> my_array;
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误:数组的大小必须是常量.有没有办法避免它?
非常感谢你.
我有
vector<int> my_vector;
vector<int> other_vector;
Run Code Online (Sandbox Code Playgroud)
用my_vector.size() == 20和other_vector.size() == 5.
鉴于int n,有0 < n < 14,我想换成子矢量(my_vector[n],myvector[n+1],...,myvector[n+4]以)other_vector.
肯定有愚蠢的代码
for(int i=0; i<5; i++)
{
my_vector[n+i] = other_vector[i];
}
Run Code Online (Sandbox Code Playgroud)
我已经完成了,但我想知道是否有更有效的方法来做到这一点.有什么建议吗?
(当然,数字20和5只是一个例子,在我的情况下,我有更大的尺寸!)
我正在编写一个需要快速Minkowski总和计算的C++软件.基于double的实现就足够了.
我评估了一些几何库,如
但我最终使用了另一个第三方库,与以前的库相比速度非常快,并且使用FIST库进行三角测量.
我的代码或多或少地以下列方式工作:
由于循环内的计算独立于循环,我将循环并行化,一切正常.
然后我决定在每个并行回合中移动Minkowski和计算:
我将结果以最佳值作为最终结果
但是第三方图书馆不再工作了.
我收到number_of_threads - 1错误消息说
断言失败.
导致断言失败的文件从运行变为运行以及从线程变为线程,但它们都是与FIST头同名的c文件(虽然我有第三方库的源代码,但我只有一个. lib和FIST库的头文件)
如前所述,我尝试在并行化代码之外计算我需要的所有Minkowski总和并使用其中的结果.这没关系.所以我几乎可以肯定问题来自FIST.
我有两个问题:
你知道FIST库是否是线程安全的吗?
如果没有,你能否建议我一个线程安全(C或更好)的C++三角测量库来取代FIST(可能具有相似的性能)?
编辑:
实际上,我不知道"线程安全"是否正是我想要的:我只需要一个能够同时计算许多独立三角测量的指令库.
我认为如果库没有全局变量,并且它有一个没有static变量的类
class triangulation
{
// no static variables
void execute_triangulation();
}
Run Code Online (Sandbox Code Playgroud)
这可能就足够了.所以我可以使用该类的不同实例并与它们的方法并行运行.
我在Visual Studio 2010中使用C#和框架4.0.
在我的项目中,有两种不同的形式,有两个FileSystemWatchers属性EnableRaisingEvent设置为false.如果我关闭Visual Studio,当我重新打开它时,我会将FileSystemWatcher属性EnableRaisingEvent设置为true.
在设计器文件中的两个表单中都有以下代码:
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
this.SuspendLayout();
this.fileSystemWatcher1.Filter = "my_filter";
this.fileSystemWatcher1.NotifyFilter = System.IO.NotifyFilters.LastWrite;
this.fileSystemWatcher1.SynchronizingObject = this;
this.fileSystemWatcher1.Changed += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed);
}
Run Code Online (Sandbox Code Playgroud)
该属性EnableRaisingEvent未设置,但默认值为false.
知道为什么我会得到这种奇怪的行为吗?
编辑
我按照Virtlink的建议,添加了以下代码:
this.fileSystemWatcher1.EnableRaisingEvents = false;
Run Code Online (Sandbox Code Playgroud)
它似乎解决了我的问题,但几天后(以及一些开放,关闭和重建项目,但没有修改fileSystemWatcher1)我发现:
在设计师,在属性fileSystemWatcher1,EnableRaisingEvents被设置回来true
在代码中,先前添加的行丢失了
我尝试转移到Visual Studio 2012(仍然是框架4.0),并且解决方法将问题解决了几天.然后我得到了与VS10相同的情况.
还有其他想法吗?
我有一个由16位数字(十六进制数字)组成的字符串,它将作为一个大数字输入到文本框中.例如,'1111222233334444".
我需要
我找到了一些方法来做到这一点,但他们只是写入控制台.因此,在用户输入该数据后,我需要具有以下内容:
string first = 1111;
string second = 2222;
string third = 3333;
string fourth = 4444.
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!