Difference between revisions of "Programming/C-Sharp"
m |
(→Programming example) |
||
(21 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
== Programming Details == | == Programming Details == | ||
− | C# is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming. Although originally developed by Microsoft for their own platform it has been | + | C# is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming. Although originally developed by Microsoft for their own platform it has been standardized and a Linux version called MONO has been developed and matured. |
The Mono project provides an open-source C# compiler, a complete open-source implementation of the Common Language Infrastructure including the required framework libraries as they appear in the ECMA specification, and a nearly complete implementation of the Microsoft proprietary .NET class libraries up to .NET 3.5. Note at this time Windows Presentation Foundation (WPF) does not exist. | The Mono project provides an open-source C# compiler, a complete open-source implementation of the Common Language Infrastructure including the required framework libraries as they appear in the ECMA specification, and a nearly complete implementation of the Microsoft proprietary .NET class libraries up to .NET 3.5. Note at this time Windows Presentation Foundation (WPF) does not exist. | ||
Line 8: | Line 8: | ||
=== Programming example === | === Programming example === | ||
− | |||
+ | <pre style="background-color: #C8C8C8; color: black; border: 2px solid #C8C8C8; font-family: monospace, sans-serif;"> | ||
using System; | using System; | ||
using MPI; | using MPI; | ||
Line 30: | Line 30: | ||
} | } | ||
} | } | ||
+ | } | ||
+ | </pre> | ||
+ | <pre style="background-color: #C8C8C8; color: black; border: 2px solid #C8C8C8; font-family: monospace, sans-serif;"> | ||
+ | using System; | ||
+ | using System.Linq; | ||
+ | using System.Threading; | ||
+ | using System.Threading.Tasks; | ||
+ | using System.Collections.Concurrent; | ||
+ | |||
+ | class MatrixCalc | ||
+ | { | ||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | Random rnd = new Random(); | ||
+ | |||
+ | int size = int.Parse(args[0]); | ||
+ | int maxVal = 255; | ||
+ | |||
+ | double[,] m1 = new double[size, size]; | ||
+ | double[,] m2 = new double[size, size]; | ||
+ | double[,] res = new double[size, size]; | ||
+ | |||
+ | for (int i = 0; i < size; i++) | ||
+ | { | ||
+ | for (int j = 0; j < size; j++) | ||
+ | { | ||
+ | m1[i, j] = rnd.Next(0, maxVal); | ||
+ | m2[i, j] = rnd.Next(0, maxVal); | ||
+ | } | ||
+ | } | ||
+ | int para = Environment.ProcessorCount; | ||
+ | |||
+ | var watch = System.Diagnostics.Stopwatch.StartNew(); | ||
+ | |||
+ | Parallel.For(0, para, worker => | ||
+ | { | ||
+ | var max = size * (worker + 1) / para; | ||
+ | var start = size * worker / para; | ||
+ | for (int row = start; row < max; row++) | ||
+ | { | ||
+ | for (int col = start; col < max; col++) | ||
+ | { | ||
+ | double temp = 0; | ||
+ | for (int ele = start; ele < max; ele++) | ||
+ | { | ||
+ | temp += m1[row, ele] * m2[ele, col]; | ||
+ | } | ||
+ | res[row, col] = temp; | ||
+ | } | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | watch.Stop(); | ||
+ | var elapsedMs = watch.ElapsedMilliseconds; | ||
+ | System.Console.WriteLine(elapsedMs); | ||
+ | } | ||
} | } | ||
Line 39: | Line 95: | ||
The following modules are available: | The following modules are available: | ||
− | * module | + | * module add mono/4.4.1 |
− | + | * module add mono/5.12.0 | |
+ | * module add mono/5.16.0.220 | ||
==== Compilation ==== | ==== Compilation ==== | ||
− | The program would be compiled in the following way | + | The program would be compiled in the following way: |
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> | ||
+ | [username@login01 ~]$ module add mono/5.12.0 | ||
+ | [username@login01 ~]$ mcs -out:cDEMO.exe cDEMO.cs | ||
</pre> | </pre> | ||
− | |||
== Usage Examples == | == Usage Examples == | ||
− | = Batch example = | + | === Batch example === |
− | <pre style="background-color: # | + | <pre style="background-color: #f5f5dc; color: black; font-family: monospace, sans-serif;"> |
#!/bin/bash | #!/bin/bash | ||
Line 65: | Line 119: | ||
#SBATCH -N 1 | #SBATCH -N 1 | ||
#SBATCH --ntasks-per-node 28 | #SBATCH --ntasks-per-node 28 | ||
− | |||
#SBATCH -o %N.%j.%a.out | #SBATCH -o %N.%j.%a.out | ||
#SBATCH -e %N.%j.%a.err | #SBATCH -e %N.%j.%a.err | ||
#SBATCH -p compute | #SBATCH -p compute | ||
#SBATCH --exclusive | #SBATCH --exclusive | ||
+ | #SBATCH --mail-user= your email address here | ||
echo $SLURM_JOB_NODELIST | echo $SLURM_JOB_NODELIST | ||
module purge | module purge | ||
− | module | + | module add mono/5.12.0 |
export I_MPI_DEBUG=5 | export I_MPI_DEBUG=5 | ||
Line 81: | Line 135: | ||
mono /home/user/CODE_SAMPLES/C-SHARP/cDEMO.exe | mono /home/user/CODE_SAMPLES/C-SHARP/cDEMO.exe | ||
+ | </pre> | ||
− | |||
− | <pre style="background-color: | + | <pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;"> |
[username@login01 ~]$ sbatch demo.job | [username@login01 ~]$ sbatch demo.job | ||
− | Submitted batch job | + | Submitted batch job 1291552 |
</pre> | </pre> | ||
− | [[ | + | == Next Steps == |
+ | |||
+ | * [https://en.wikibooks.org/wiki/C_Sharp_Programming https://en.wikibooks.org/wiki/C_Sharp_Programming] | ||
+ | * [[programming/OpenMP|OpenMP]] | ||
+ | * [[programming/OpenMPI|OpenMPI]] | ||
+ | |||
+ | {{Languagespagenav}} |
Latest revision as of 13:49, 31 August 2023
Contents
Programming Details
C# is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming. Although originally developed by Microsoft for their own platform it has been standardized and a Linux version called MONO has been developed and matured.
The Mono project provides an open-source C# compiler, a complete open-source implementation of the Common Language Infrastructure including the required framework libraries as they appear in the ECMA specification, and a nearly complete implementation of the Microsoft proprietary .NET class libraries up to .NET 3.5. Note at this time Windows Presentation Foundation (WPF) does not exist.
Programming example
using System; using MPI; class Ring { static void Main(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; if (comm.Rank == 0) { // program for rank 0 } else // not rank 0 { // program for all other ranks } } } }
using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Collections.Concurrent; class MatrixCalc { static void Main(string[] args) { Random rnd = new Random(); int size = int.Parse(args[0]); int maxVal = 255; double[,] m1 = new double[size, size]; double[,] m2 = new double[size, size]; double[,] res = new double[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { m1[i, j] = rnd.Next(0, maxVal); m2[i, j] = rnd.Next(0, maxVal); } } int para = Environment.ProcessorCount; var watch = System.Diagnostics.Stopwatch.StartNew(); Parallel.For(0, para, worker => { var max = size * (worker + 1) / para; var start = size * worker / para; for (int row = start; row < max; row++) { for (int col = start; col < max; col++) { double temp = 0; for (int ele = start; ele < max; ele++) { temp += m1[row, ele] * m2[ele, col]; } res[row, col] = temp; } } }); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; System.Console.WriteLine(elapsedMs); } }
Modules Available
The following modules are available:
- module add mono/4.4.1
- module add mono/5.12.0
- module add mono/5.16.0.220
Compilation
The program would be compiled in the following way:
[username@login01 ~]$ module add mono/5.12.0 [username@login01 ~]$ mcs -out:cDEMO.exe cDEMO.cs
Usage Examples
Batch example
#!/bin/bash #SBATCH -J openmpi-single-node #SBATCH -N 1 #SBATCH --ntasks-per-node 28 #SBATCH -o %N.%j.%a.out #SBATCH -e %N.%j.%a.err #SBATCH -p compute #SBATCH --exclusive #SBATCH --mail-user= your email address here echo $SLURM_JOB_NODELIST module purge module add mono/5.12.0 export I_MPI_DEBUG=5 export I_MPI_FABRICS=shm:tmi export I_MPI_FALLBACK=no mono /home/user/CODE_SAMPLES/C-SHARP/cDEMO.exe
[username@login01 ~]$ sbatch demo.job Submitted batch job 1291552