Last active
August 29, 2015 14:05
-
-
Save johnwalley/b28d5d61d98bf6427dc1 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 MatrixVectorMultiply1 | |
{ | |
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 j = 0; j < dim; j++) | |
{ | |
for (int i = 0; i < dim; i++) | |
{ | |
vecOut[j] += matrix[i, j] * vecIn[j]; | |
} | |
} | |
watch.Stop(); | |
Console.WriteLine("Elapsed time for example #1: " + 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