❔ is there any way i can run my csharp console app net framwork program executable on my gpu?

Hi, i want to know if there is any way i can run my c# console app net framwork executable file on my gpu insted of my cpu becuase i think my gpu would run the program i wrote faster then my cpu.
48 Replies
Anton
Anton2y ago
why do you think so
GlitchYourDevice
GlitchYourDeviceOP2y ago
becuase its a program that does other stuff butt stuff almost like a cryptominer does and a cryptominer works best with the gpu
Sossenbinder
Sossenbinder2y ago
GitHub
GitHub - Sergio0694/ComputeSharp: A .NET library to run C# code in ...
A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀...
Anton
Anton2y ago
write a compute shader
GlitchYourDevice
GlitchYourDeviceOP2y ago
what is that
Anton
Anton2y ago
you do work on the gpu via shaders that abstraction they sent above generates said shaders from .NET IL code dynamically I'm guessing
GlitchYourDevice
GlitchYourDeviceOP2y ago
can i not just put all my c# code it to some kind of converter and then use the converted code so it will be runned on my gpu
Anton
Anton2y ago
that's what they sent above but it's not as simple as that gpus are highly multithreaded which means the program has to be written with that in mind converting an arbitrary program doesn't give you any benefit shaders are programs that run e.g. per pixel on the screen, each on a separate thread, in a mini context
GlitchYourDevice
GlitchYourDeviceOP2y ago
ok but this is the program that i want to convert using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Globalization; using System.Runtime.CompilerServices; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { string filepath = @"D:\text1.txt"; string filepath2 = @"D:\text2.txt"; List<string> lines = File.ReadAllLines(filepath).ToList(); List<string> lines2 = File.ReadAllLines(filepath2).ToList(); for (int i = 0; i < lines.Count; i++) { for (int j = 0; j < lines2.Count; j++) { if (lines[i] == lines2[j].Split(':')[0]) { Console.WriteLine(lines2[j]); } } } Console.WriteLine("\nDone"); Console.ReadLine(); } } }
GlitchYourDevice
GlitchYourDeviceOP2y ago
this works with the cpu becuase its not many rows in text1 and text2
Aaron
Aaron2y ago
uhhh I don't think this is exactly what GPUs are built to do there are other ways to optimize this though
GlitchYourDevice
GlitchYourDeviceOP2y ago
but i have a other "text1" doucument with 8k rows and the "txt2 document" has 8000000 rows with my 8k txt1 and 8000k txt2 document this takes to long with the cpu
Aaron
Aaron2y ago
you could get rid of the outer loop over lines, for example and use a HashSet with HashSet.Contains instead
GlitchYourDevice
GlitchYourDeviceOP2y ago
what do you mean? where in my code?
Aaron
Aaron2y ago
you have for (int i = 0; i < lines.Count; i++) you could get rid of that loop
GlitchYourDevice
GlitchYourDeviceOP2y ago
ok
Aaron
Aaron2y ago
have HashSet<string> lines = new HashSet<string>(File.ReadAllLines("D:\text1.txt"));
GlitchYourDevice
GlitchYourDeviceOP2y ago
ok
Aaron
Aaron2y ago
then do lines.Contains(lines2[j].Split(':')[0]) in the if
GlitchYourDevice
GlitchYourDeviceOP2y ago
ok
Aaron
Aaron2y ago
and that should be significantly faster you can also multithread this on your CPU instead of trying to make it run on a GPU
GlitchYourDevice
GlitchYourDeviceOP2y ago
i will try it and write back here if i dont get it to work using System.Text; using System.Threading.Tasks; using System.IO; using System.Globalization; using System.Runtime.CompilerServices; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { string filepath = @"D:\text1.txt"; string filepath2 = @"D:\text2.txt"; List<string> lines2 = File.ReadAllLines(filepath2).ToList(); HashSet<string> lines = new HashSet<string>(File.ReadAllLines("D:\text1.txt"));
for (int j = 0; j < lines2.Count; j++) { if (lines.Contains(lines2[j].Split(':')[0])) { Console.WriteLine(lines2[j]); } } Console.WriteLine("\nDone"); Console.ReadLine(); } } } that does not work at all
Aaron
Aaron2y ago
it's doing almost the same thing as the code from before though it doesn't print multiple times for one row if the same line exists in text1 more than once
GlitchYourDevice
GlitchYourDeviceOP2y ago
it is no duplicates so that do i not need to worry about with my bigger txt documents
Aaron
Aaron2y ago
what doesn't work about it
GlitchYourDevice
GlitchYourDeviceOP2y ago
i cant even run it can you just please rewrite the code and send it here i have never even used hashset before
Aaron
Aaron2y ago
why can't you run it
GlitchYourDevice
GlitchYourDeviceOP2y ago
it said that varible lines is already used
Aaron
Aaron2y ago
what
GlitchYourDevice
GlitchYourDeviceOP2y ago
System.ArgumentException: 'Illegal characters in path.'
Aaron
Aaron2y ago
oh because the thing in the ReadAllLines should've just been filepath
Torn
Torn2y ago
Gonna need to escape the backslash I think \\
Aaron
Aaron2y ago
instead of the string
GlitchYourDevice
GlitchYourDeviceOP2y ago
ok
Aaron
Aaron2y ago
currently there's a tab in the string lmao
GlitchYourDevice
GlitchYourDeviceOP2y ago
this is the code now using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Globalization; using System.Runtime.CompilerServices; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { string filepath = @"D:\text1.txt"; string filepath2 = @"D:\text2.txt"; List<string> lines2 = File.ReadAllLines(filepath2).ToList(); HashSet<string> lines = new HashSet<string>(File.ReadAllLines("D:\text1.txt"));
for (int j = 0; j < lines2.Count; j++) { if (lines.Contains(lines2[j].Split(':')[0])) { Console.WriteLine(lines2[j]); } } Console.WriteLine("\nDone"); Console.ReadLine(); } } } what should i change
Aaron
Aaron2y ago
did you fix the ArgumentException
GlitchYourDevice
GlitchYourDeviceOP2y ago
thanks it worked now but i have no idea how it worked and why is it so much faster?
Anton
Anton2y ago
it had quadratic complexity, but you made it linear or is it n logn
Aaron
Aaron2y ago
HashSets can check whether they contain something in O(1) time
GlitchYourDevice
GlitchYourDeviceOP2y ago
ok
Aaron
Aaron2y ago
meaning no matter how many lines there are in text1, it will take the same amount of time to do .Contains that takes the number of iterations you do from 8k * 8000000 to just 8000000 which is much smaller! you were right, it's linear based on the line count of text2 understanding time complexity, and how to reduce it, is one of the best ways to become better at optimizing code
Anton
Anton2y ago
yeah a search tree would've been n log n, constructing hash sets is linear
Aaron
Aaron2y ago
not sure why you'd use a binary tree here
Anton
Anton2y ago
you wouldn't, I was just reminding myself of its complexity
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server