Last active
August 29, 2015 14:05
-
-
Save johnwalley/eaa38c1852e94c0683aa to your computer and use it in GitHub Desktop.
Simple matrix-vector multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
namespace MatrixVectorMultiply | |
{ | |
class MatrixVectorMultiply2 | |
{ | |
static void Main(string[] args) | |
{ | |
const int dim = 1024 * 8; | |
var matrix = new float[dim, dim]; | |
var vecIn = new float[dim]; | |
var vecOut = new float[dim]; | |
Init(dim, matrix, vecIn); | |
Stopwatch watch = Stopwatch.StartNew(); | |
for (int i = 0; i < dim; i++) | |
{ | |
for (int j = 0; j < dim; j++) | |
{ | |
vecOut[i] += matrix[i, j] * vecIn[i]; | |
} | |
} | |
watch.Stop(); | |
Console.WriteLine("Elapsed time for example #2: " + watch.ElapsedMilliseconds + " ms"); | |
} | |
private static void Init(int dim, float[,] matrix, float[] vecIn) | |
{ | |
for (int i = 0; i < dim; i++) | |
{ | |
for (int j = 0; j < dim; j++) | |
{ | |
matrix[i, j] = i*j; | |
} | |
} | |
for (int i = 0; i < dim; i++) | |
{ | |
vecIn[i] = i; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment