Skip to content

Instantly share code, notes, and snippets.

@vkvenkat
Last active September 19, 2018 01:10
Show Gist options
  • Save vkvenkat/f35b8ff9552e33c6a11453bcea7d25fa to your computer and use it in GitHub Desktop.
Save vkvenkat/f35b8ff9552e33c6a11453bcea7d25fa to your computer and use it in GitHub Desktop.
Simple matrix multiplication sample - With jagged arrays replacing rectangular arrays
/*
Copyright 2018 Intel Corporation.
The source code, information and material ("Material") contained herein
is owned by Intel Corporation or its suppliers or licensors, and title
to such Material remains with Intel Corporation or its suppliers or
licensors. The Material contains proprietary information of Intel or its
suppliers and licensors. The Material is protected by worldwide
copyright laws and treaty provisions. No part of the Material may be
used, copied, reproduced, modified, published, uploaded, posted,
transmitted, distributed or disclosed in any way without Intel's prior
express written permission. No license under any patent, copyright or
other intellectual property rights in the Material is granted to or
conferred upon you, either expressly, by implication, inducement,
estoppel or otherwise. Any license under such intellectual property
rights must be express and approved by Intel in writing.
Unless otherwise agreed by Intel in writing, you may not remove or alter
this notice or any other notice embedded in Materials by Intel or
Intel's suppliers or licensors in any way.
*/
using System;
namespace matmuljagged
{
class Program
{
const int rows = 10;
const int columns = 10;
static void Main(string[] args)
{
int[][] op1 = new int[rows][];
int[][] op2 = new int[rows][];
int[][] result = new int[rows][];
for (var j = 0; j < rows; j++)
{
op1[j] = new int[columns];
op2[j] = new int[columns];
result[j] = new int[columns];
}
for (var i = 0; i < 2000000; i++)
{
Initialize(ref op1, ref op2);
Multiply(op1, op2, ref result);
}
Console.WriteLine("\nOperand1:");
Display(op1);
Console.WriteLine("\nOperand2:");
Display(op2);
Console.WriteLine("\nResult:");
Display(result);
}
static void Initialize(ref int[][] matrixA, ref int[][] matrixB)
{
Random rnd = new Random();
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
matrixA[i][j] = rnd.Next(0, 9);
}
}
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
matrixB[i][j] = rnd.Next(0, 9);
}
}
}
static void Multiply(int[][] matrixA, int[][] matrixB, ref int[][] matrixC)
{
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
matrixC[i][j] = 0;
for (var inner = 0; inner < columns; inner++)
{
matrixC[i][j] += matrixA[i][inner] * matrixB[inner][j];
}
}
}
}
static void Display(int[][] sampleArray)
{
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < columns; j++)
{
Console.Write("{0, 5} ", sampleArray[i][j]);
}
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment