Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple matrix-vector multiplication
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
You can’t perform that action at this time.