我在谷歌搜索提供一些简单的OpenMp算法的页面.可能有一个例子来计算巨大数据阵列的最小值,最大值,中值,平均值,但我无法找到它.
至少我通常会尝试将数组划分为每个核心的一个块,然后进行一些边界计算以获得完整数组的结果.
我只是不想重新发明轮子.
补充说明:我知道有成千上万的例子可以简单地减少.例如,计算PI.
const int num_steps = 100000;
double x, sum = 0.0;
const double step = 1.0/double(num_steps);
#pragma omp parallel for reduction(+:sum) private(x)
for (int i=1;i<= num_steps; i++){
x = double(i-0.5)*step;
sum += 4.0/(1.0+x*x);
}
const double pi = step * sum;
Run Code Online (Sandbox Code Playgroud)
但是当这些算法不可用时,几乎没有留下用于减少算法的例子.
我创建了这两个方法来将Native utf-8字符串(char*)转换为托管字符串,反之亦然.以下代码完成了这项工作:
public IntPtr NativeUtf8FromString(string managedString)
{
byte[] buffer = Encoding.UTF8.GetBytes(managedString); // not null terminated
Array.Resize(ref buffer, buffer.Length + 1);
buffer[buffer.Length - 1] = 0; // terminating 0
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
return nativeUtf8;
}
string StringFromNativeUtf8(IntPtr nativeUtf8)
{
int size = 0;
byte[] buffer = {};
do
{
++size;
Array.Resize(ref buffer, size);
Marshal.Copy(nativeUtf8, buffer, 0, size);
} while (buffer[size - 1] != 0); // till 0 termination found
if (1 == size)
{
return ""; …
Run Code Online (Sandbox Code Playgroud) 一个常见的国际问题是字符串中表示的双值的转换.这个东西在很多地方都有发现.
从调用的csv文件开始
comma separated
Run Code Online (Sandbox Code Playgroud)
要么
character separated
Run Code Online (Sandbox Code Playgroud)
因为有时它们会被存储起来
1.2,3.4
5.6,6.4
Run Code Online (Sandbox Code Playgroud)
在英语地区或
1,2;3,4
5,6;6,4
Run Code Online (Sandbox Code Playgroud)
例如德国地区.
从这个背景来看,有必要知道大多数std ::方法都依赖于语言环境.因此在德国,他们会将"1,2"读作1.2并将其写回"1,2",但使用英语操作系统时,它将"1,2"读为1并将其写回为"1".
由于语言环境是应用程序的全局状态,因此将其切换到其他设置并不是一个好主意.当我必须在英语机器上读取德语CSV文件或反之亦然时,我们遇到了一些问题.
在所有机器上编写行为相同的代码也很困难.C++流允许每个流的区域设置.
class Punctation : public numpunct<wchar_t>
{
public:
typedef wchar_t char_type;
typedef std::wstring string_type;
explicit Punctation(const wchar_t& decimalPoint, std::size_t r = 0) :
decimalPoint_(decimalPoint), numpunct<wchar_t>(r)
{
}
Punctation(const Punctation& rhs) :
decimalPoint_(rhs.decimalPoint_)
{
}
protected:
virtual ~Punctation()
{
};
virtual wchar_t do_decimal_point() const
{
return decimalPoint_;
}
private:
Punctation& operator=(const Punctation& rhs);
const wchar_t decimalPoint_;
};
...
std::locale newloc(std::locale::classic(), new Punctation(L','));
stream.imbue(newloc);
Run Code Online (Sandbox Code Playgroud)
将允许您使用std …
void printLine(const wchar_t* str, ...)
{
// have to do something to make it work
wchar_t buffer[2048];
_snwprintf(buffer, 2047, ????);
// work with buffer
}
printLine(L"%d", 123);
Run Code Online (Sandbox Code Playgroud)
我试过了
va_list vl;
va_start(vl,str);
Run Code Online (Sandbox Code Playgroud)
这样的事情,但我找不到解决方案.