This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are C# arrays thread safe?
I have a program that is single-threaded at the moment, which basically puts lots of computed data into a multidimensional array (e.g. double[,] ; string[,] ).
Is it possible to assign segments of this array to different threads ? More precisely, if I make sure only one thread will write at a given coordinate, will there be some lock mechanism triggered ?
In terms of concurrency problems, you will be fine as long as your threads do not read or write to the same portion of your array concurrently. You may see slowdowns because of "False Sharing" hazard, though, so you may want to be on the lookout for unexpected slow-downs when the number of threads increases.
if I make sure only one thread will write at a given coordinate
Then you are safe. Assuming you don't resize the array etc.
If you are now using a for loop you can probably simply switch to Parallel.For(0, n, method)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I'm trying to get into multithreading by trying to do matrix multiplication and my problem is, how I would get all sub matrixes from a matrix.
My matrix variable is a int[,].
Example, if I have a matrix by 100 x 100, how would i get 10 of 10 x 10 sub matrix. And is it possible that user can choose to how many equal parts to cut up the matrix even if I the matrix is not a square ex. 400 x 300?
Is it even the right way to do it, by calculate on the sub matrixes and then add them together when done?
how would i get 10 of 10 x 10 sub matrix
You would do a double loop, copying each value from the original matrix to the new sub matrix.
Is it even the right way to do it, by calculate on the sub matrixs and then add them together when done?
The normal way to multiply matrices is with a triple loop, as shown in this answer. It should be fairly trivial to convert the outer loop to a parallel.For loop, since all calculations are independent from each other. This avoids any need to process individual sub matrices, and let the framework deal with partitioning the work.
However, things like this is typically fairly cache sensitive. A matrix will be stored in memory as sequential values, either row or column major. Accessing sequential values will be very cache friendly, but accessing non sequential values will not be. So you might want to copy a full row/column to a temporary array to ensure all subsequent accesses are sequential. If using a parallel loop you should probably use one of the overloads that give you a thread local array to use. There more things one can do with cache optimizations, and SIMD intrinstics but that is probably best left as a later exercise.
There are algorithms with a lower algorithmic complexity that does work on submatrices, but in my experience it will be fairly tricky to make this actually faster in c# than a cache-optimized triple loop.
Keep in mind to measure the performance of your method. I would also suggest comparing your performance with some well optimized existing library to get some sense of how performant your implementation is.
This question already has answers here:
C# reference of 2nd dim array to 1d array
(1 answer)
What is the most efficient way of storing data between a multi-dimension array, and a single array?
(3 answers)
Closed 2 years ago.
I know that there is already a question about converting a multidimensional array into a one dimensional one, but all of the answers to it are inefficient. All of them are based around making a copy of the array, which is unnecessary. A multidimensional array (not jagged) is stored in a contiguous block of memory, so converting back and forth between a one dimensional interpretation of this block and a multidimensional interpretation should not involve copying the whole array, but instead should be essentially free. It should actually be possible to have the two arrays share the same memory, so that when one gets updated, the other one also does.
Can this be done in C#?
No, you cannot. Quite apart from anything else, arrays, like many things in .NET are objects. That means that they have an object header positioned immediately before other memory used for storing their representation.
Logically, you cannot have two different object headers, for two different types of objects, occupying the same location in memory.
Well, the data itself if pretty much stored the same way, but the wrapping around that - the managed array type - is what's stopping you from accessing it directly.
The managed array, as Damien pointed out, has different headers for one or multidimensional kinds, for example, multidimensional array's header has more fields - to store the dimension values.
The arrays themselves are maintained by IL and GC and their "data memory" part could not be "frozen" so that the headers could be swapped from one to another, if you'd want to write directly to RAM to switch them.
I'm also concerned that there is an XY problem. Why indeed are you trying to convert one array to another? Is there a reason you cannot just use this instead?
public static T Get<T>(this T[] array, int x, int y, int maxX)
{
return T[x + y * maxX];
}
Or even write your own wrapper around an array and provide both Get[index/indices] methods from there?
This question already has an answer here:
How to allocate arrays on the stack for performance gains?
(1 answer)
Closed 7 years ago.
I am writing a program in C#. Everything centers around a static 2D int array which is 400x6 elements. Just a few values will be updated just once every minute. But after each minute's updates, dozens of functions will read the values millions of times to compute "pattern scores". The faster the calculations, the more distinct functions I can cram in there. Realistically I can allow 30 seconds for this scoring process. Is there a way to allocate the static array to the stack, and if so, would this help the speed? Thanks.
Yes, you can alloc arrays on the stack in C# using "stackalloc" in "unsafe mode", but benchmarks shows a limited performance gain and the risk is that you hit the 1Mb stack size limit... which will give you a... StackOverflow(tm)!
Here is a good article on the subject:
http://blogs.microsoft.co.il/sasha/2013/10/17/on-stackalloc-performance-and-the-large-object-heap/
You can use "stackalloc" to alloc array directly on the stack.
Some documentation about :
https://msdn.microsoft.com/en-us/library/aa664785(v=vs.71).aspx
You can also use an implementation of Hamming weight which is describe here :
How to allocate arrays on the stack for performance gains?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
When should I allocate a new thread to the task?
I have one task to compute 100k of equations and store the result into one array, and the second one to sort it. Should I stick to 2 threads, taking into account that I make it a thread-safe code, or can I assign like 3 threads to calculate one third of 100k equations and a fourth one to deal with sorting? Or just 2 threads?
Also, I have a 4 core processor, what happens if I bring the program with 4 threads onto another pc with 2 cores?
Thank you!
First of all, having two threads for the two tasks you describe (calculating and sorting) is useless, since you can only sort the results when all calculations are done, assuming you want to sort by the results.
For the calculations themselves, it depends on the weight of the calculation. Threads allow you to execute them simultaneously, but you also got a little overhead. Having more than one thread on one core is slower than having just one thread, since you got the overhead of switching between thread, without the benefit of simultaneous execution.
Also, you will need a thread safe version of an array (or list), which might be a bit slower because it may to synchonise access to it.
So I think a better solution would be to store the results in one array per thread, let the threads calculate independently, and only after they are all done combine the arrays. I must admit I don't know if you can assign a single thread to a single core. If so, I would create one thread per core.
When dividing the calculations between the threads, don't cut the array in N equal pieces. It could be that one of your cores is very busy with a demanding thread from another process. If that is the case, then a thread of your process will get hardly any time. So it's better to assign small pieces, so if a thread is slower, it will just calculate less pieces of the source array. If you use a thread safe counter, each thread can just pick the next item after each calculation.
This question already has answers here:
Multi Threading [closed]
(5 answers)
Closed 9 years ago.
How can I measure a code if it is thread-safe or not?
may be general guidelines or best practices
I know that the code to be threading safe is to work across threads without doing unpredictable behavior, but that's sometimes become very tricky and hard to do!
I came up with one simple rule, which is probably hard to implement and therefore theoretical in nature. Code is not thread safe if you can inject some Sleep operations to some places in the code and so change the outcome of the code in a significant way. The code is thread safe otherwise (there's no such combination of delays that can change the result of code execution).
Not only your code should be taken into account when considering thread safety, but other parts of the code, the framework, the operating system, the external factors, like disk drives and memory... everything. That is why this "rule of thumb" is mainly theoretical.
I think The best answer would be here
Multi Threading, I couldn't have notice such an answer before writing this question
I think it is better to close is it !
thanks
Edit by 280Z28 (since I can't add a new answer to a closed question)
Thread safety of an algorithm or application is typically measured in terms of the consistency model which it is guaranteed to follow in the presence of multiple threads of execution (or multiple processes for distributed systems). The two most important things to examine are the following.
Are the pre- and post-conditions of individual methods preserved when multiple threads are used? For example, if your method "adds an element to a dynamically-sized list", then one post condition would be that the size of the list increases by 1 as a result of the add method. If your algorithm is thread-safe, then calling the add method 2 times would result in the size increasing by exactly 2, regardless of which threads were used for the add operations. On the other hand, if the algorithm is not thread-safe, then using multiple threads for the 2 calls could result in anything, ranging from correctly adding the 2 items all the way to the possibility of crashing the program entirely.
When changes are made to data used by algorithms in the program, when do those changes become visible to the other threads in the system. This is the consistency model of your code. Consistency models can be very difficult to understand fully so I'll leave the link above as the starting place for your continued learning, along with a note that systems guaranteeing linearizability or sequential consistency are often the easiest to work with, although not necessarily the easiest to create.