简体   繁体   English

在 c# 中使用 System.Buffers (ArrayPool) 创建矩阵

[英]Creating a matrix with System.Buffers (ArrayPool) in c#

I'm looking to create a matrix using System.Buffers or ArrayPool in C# is this possible?我想在 C# 中使用 System.Buffers 或 ArrayPool 创建一个矩阵,这可能吗?

All of the examples appear to be single dimension arrays, or for jagged array of fixed size.所有示例似乎都是单维 arrays,或者是固定大小的锯齿状数组。

I don't want to create a mulit-dimension array (jagged array) I want to create a matrix because it's going to be used to pass some data via interop.我不想创建多维数组(锯齿状数组)我想创建一个矩阵,因为它将用于通过互操作传递一些数据。

eg:例如:

var matrix = new object[9000,9000];

The short answer is "no, you need to linearize";简短的回答是“不,你需要线性化”; ArrayPool<T> only deals with vectors, ie single-dimension arrays with zero base-offset; ArrayPool<T>仅处理向量,即单维 arrays,基偏移为零; this is partly because they're the most common scenario, and partly because of cache hits - ie if you only need to worry about a single size dimension, it is easy to pool arrays with a good chance of them reused, but if you need to worry about two (or more) dimensions, the semantics for reuse become a lot more complex, and the number of "hits" (ie reused buffers) drops significantly (in the general case).这部分是因为它们是最常见的情况,部分是因为缓存命中 - 即,如果您只需要担心单个尺寸维度,则很容易将 arrays 合并在一起,并且很有可能会被重用,但如果您需要担心两个(或更多)维度,重用的语义变得更加复杂,并且“命中”(即重用缓冲区)的数量显着下降(在一般情况下)。

Fundamentally, though, the actual payload space of an array is identical in all cases - it is just a chunk of memory the size of the multiple of all the dimensions - so a T[n,m] is the same payload size as a T[n*m] .但是,从根本上说,数组的实际有效载荷空间在所有情况下都是相同的——它只是 memory 的一块,大小是所有维度的倍数——所以T[n,m]T[n*m]有效载荷大小相同T[n*m] When talking about interop, you're ultimately just passing the root address of that memory anyway , and the consumer will need to de-linearize, so: perhaps just use a vector in the first place, and de-linearize yourself using whatever dimension order needed by the interop layer.在谈论互操作时,您最终只是传递了memory的根地址,而消费者将需要去线性化,因此:也许首先使用向量,然后使用任何维度顺序使自己去线性化互操作层需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM