Multithreading and get X sub matrixes [closed] - c#

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.

Related

Algorithms to brute force function inputs to find maximum [closed]

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 4 years ago.
Improve this question
There is a function like CalculateProfit(decimal a, decimal b, float c, TimeSpan d) and its each input parameter has minimum, maximum and initial value settings.
Its output is smooth but not linear, it has multiple peaks and falls. I want to bruteforce its inputs and find maximum possible output. How to optimize this without trying each possible combination? Maybe some kind of binary search?
I think the algorithm should use big delta steps at start to find most peaks and then tweak values with small deltas. Also I would bruteforce one input until I find best output and then try same for next inputs, then go back to tweaking first input and so on.
Update: the function is a complex algorithm which performs analysis on markets historical data (so it's not just a formula). Therefore I'm asking for some bruteforce optimizations, not trying to "solve" it as an equation.
You need to read about partial differential equations solvers of 2 or more variables.
https://math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/min_max/min_max.html
Then you need to study one algorithm that can solve it, Finite Volume and Spectral Method are the most commonly used in Simulation.
https://en.wikipedia.org/wiki/Numerical_partial_differential_equations
You can find easy solutions on Matlab if you are interested in just solving your problem. C# can call Matlab functions with some setup.

Independent Variables and Array performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to know what is more optimal, A number N of private integer variables, or a single array containing N integer values.
When you use an array, that is a plus indirection. The array will be allocated at a separate part of the memory, so when you first access it, your code first obtains its address, then it is able to read out its content. It also needs some indexing, but that is done extremly fast by the CPU. However, .NET is a safe environment and it will do a check whether you use a valid array index. It adds additional time.
When you use separate variables, these will be encompassed by your object instance and no indirection is needed. Also, no index bound check is needed.
Moreover, you cannot name nicely the Nth element of an array, but you can give good names for individual variables. So your code will be readable.
As others mentioned, you shouldn't do this kind of optimalizations, the compiler/jitter take care of it. The compiler knows several common use cases and has optimialization strategy for that. If you start doing tricky things, the compiler will not recognize your intention and cannot make the optimalization for you.

When to allocate a new thread? [closed]

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.

Number of Parameter Passed to Function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I want to know how many parameters can be passed to function, I mean what is good programming practice, regarding passing the parameters to function?
Code Complete suggests a maximum of 7. This is because of The Magical Number Seven, Plus or Minus Two:
...the number of objects an average human can hold in working memory is 7 ± 2; this is frequently referred to as Miller's Law.
Here's an excerpt from Code Complete 2nd Edition:
Limit the number of a routine’s parameters to about seven
Seven is a magic number for people’s comprehension. Psychological research has found that people generally cannot keep track of more than about seven chunks of information at once (Miller 1956). This discovery has been applied to an enormous number of disciplines, and it seems safe to conjecture that most people can’t keep track of more than about seven routine parameters at once.
The fewer the better, but only if it still makes sense. I've never heard of a standard number of params to be passed, but I have heard of ways to keep them down better.
For example, don't do this:
public void DoSomething(string name, int age, int weight, ...) { }
but rather:
public void DoSomething(Person person) { }
but hopefully that goes without saying. But also, I would recommend not creating a weird class just to trim down the parameter count.
IMHO 5 at MAX.
6 is too much for me and 7 overwhelming!
According to Clean Code - maximum 3
If you have many things you would like to pass to a function you may want to look at some other means of transferring that data as opposed to simple parameter passing. For example in certain cases it may be better to generate an XML file and then pass values related to getting data around that XML file. If you are running a web app it may be simply passing data through sessions or post rather than get or function calls that will simplify your life.
Also you may want to store some of that information as member variables.
I would recommend no more than 4. You don't want your lines to get much longer than 30 characters long unless you are generating some massive string, but even then it becomes really unreadable and gross (although necessary especially for javascript).
It's good programming practice to write programs so that they are easy to read. Personally I try not to write functions which have more parameters than can be displayed on one line on the screen. Usually that is no more than five or six parameters at most.
some ARM compilers pass three or less parameters using registers and any more than three are stacked. The stacked type call is slower than the call using registers so in this case you should use three or less parameters, for speed.
Depending on the architecture, more than 1-3 will cause passing on the stack. This is slower than passing via registers. From a performance standpoint, it is best to pass either a pointer to a wrapper class or a pointer to a struct. This ensures that only one value is passed in and saves some writes/reads to memory.
If you don't know how many parameters you are going to pass to a function use param for sending variable arguments to a method.

What is the best way to represent a large field of objects while using minimal resources? [closed]

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 7 years ago.
Improve this question
all I am looking to develop a project in unity, it is for android! I was wondering if I could get some clarity on a few things. My problem involves me trying to creating a universe of stars, 150,000 individual stars to be exact, granted there would only be a certain percentage in view at any one time. What is the most efficient structure for being able to convince the user of a realistic environment while keeping the overhead to a minimum since it will be on a phone?
What type of objects do I want to use to represent the masses of stars vs. the likes of stars in close proximity that require finer details?
What sort of threading structures should I consider while planning this project?
How easily does a project port from unity to android, in such scenarios?
Any help is much appreciated as I am looking to better develop with unity, cheers
I would suggest not tracking all 150,000 stars, but only the ones that are in view. When the field of view changes, use a random number generator to define the stars that have just entered it, and drop from memory the ones that have left. To preserve consistency, you might want to retain the stars for a short period around the current field of view, if the user can do rapid switches in direction.
As for threading, that's less a function of the number of stars you are tracking, and more a function of what it is that you are doing with them - something you didn't mention.
1) This question is mainly a game development question and not unity regarding. I just point you in the direction, as a complete answer would be to much. Normally if you need to know where you are in a 3D scene with infinite objects or close (150k is close), you would use a octree for orientation. Constructed like a map, each node of the tree points a direction (West, South, Nord, East, NNW, ...) Then you each of your stars gets 1 node, and you can calculate what is where and how much do you want to see. More information can be found on google. (Quite complicated topic jfyi)
2) Dedicated to 1) with a mix of entity/component design. You will know what I mean after 1) is clear to you.
3) Absolutly Multithreaded Asynchron. 1 Thread Update, 1 Thread Draw, Few Worker Threads (position, ...)
4) The port of Unity Engine is actually working really good. Of course you should have an android peripherial to test and debug on, but most of the time, it will work for you.

Categories