3 Replies - 429 Views - Last Post: 09 April 2011 - 01:41 PM Rate Topic: -----

#1 sioannou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-April 11

Multithreading _pass by reference

Posted 09 April 2011 - 11:43 AM

Hi this is my first post here , although i am reading the forums quite often and this problem turn to be a big headache for me so i would be very glad if anyone could help me.

As you can see through my code , I am calling class Calculation through the function multithread_calculation()(which is in another class) in order to implement a multithreading calculation of two matrices. The problem is, that i need the function calculate_product to return the array with the result. Or the other possible solution is to pass it by reference. None of these tactics are working so I am kind of lost. Thanks .

PS : I tried to use the ParametirizedThread instead of ThreadStart but guess i don't know how to use it correct.



    class Calculation
    {
        int[,] arrayA;
        int[,] arrayB;
        
        int rows, cols,k;

        public Calculation(int [,]arrayA,int [,]arrayB,int rows,int cols,int k)
        {
            this.arrayA = arrayA;
            this.arrayB = arrayB;
            this.rows = rows;
            this.cols = cols;
            this.k = k;
            

        }
        public void calculate_product()
        {
        

            
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    for (int l = 0; l < k; l++)
                        arrayC[i, j] += arrayA[i, l] * arrayB[l, j];
                }
              
               
        
            }

            

        }//end of function


 private void multithread_calculation()
        {

            int[,] arrayC = new int[matrix1_rows, matrix2_cols];
            //create an array of threads
            Thread[] t;

            //create object to be calculated
            Calculation[] calc;


            calc = new Calculation[10];
            t = new Thread[10];
            Stopwatch stop = new Stopwatch();
            stop.Start();

            for (int i = 0; i < 10; i++)
            {
                //create an object
                calc[i] = new Calculation(arrayA, arrayB, matrix1_rows, matrix2_cols, k);


                
                ThreadStart ts = new ThreadStart(calc[i].calculate_product);
               
             


                t[i] = new Thread(ts);

            }
            //start all threads
            for (int i = 0; i < 10; i++)
            {
                t[i].Start();
                t[i].Join();
            }


            stop.Stop();
            long seconds = stop.ElapsedMilliseconds;
            MessageBox.Show("Operation Completed in: " + seconds.ToString());


        }





This post has been edited by sioannou: 09 April 2011 - 11:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Multithreading _pass by reference

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Multithreading _pass by reference

Posted 09 April 2011 - 12:18 PM

Here's a quick example:

using System;
using System.Threading;

namespace ThreadTest
{
    public class ThreadData {
        public ThreadData (int i) {
            Val = i;
        }
        public int Val { get; set; }
    }

    public class ThreadTest {
        public static void Square(object threadData) {
            ThreadData td = (ThreadData)threadData;
            td.Val *= td.Val;
        }

        public static void Main() {
            int i = 0;

            Thread [] threads = new Thread[10];
            for (i = 0; i < threads.Length; ++i) {
                threads[i] = new Thread(Square);
            }

            ThreadData [] td = new ThreadData[threads.Length];

            for(i = 0; i < threads.Length; ++i) {
                td[i] = new ThreadData(i + 1);
                threads[i].Start(td[i]);
            }

            foreach (var thread in threads) {
                thread.Join();
            }

            for(i = 0; i < td.Length; ++i) {
                Console.WriteLine("{0}: {1}", i, td[i].Val);
            }

        }
    }
}



Result:
mono ThreadTest.exe 
0: 1
1: 4
2: 9
3: 16
4: 25
5: 36
6: 49
7: 64
8: 81
9: 100


Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Multithreading _pass by reference

Posted 09 April 2011 - 12:33 PM

For a multi-threaded Matrix Multiplication you can look here. I'm currently working on a multi-threaded optimized version of the Strassen method (and a full Matrix class).
Was This Post Helpful? 0
  • +
  • -

#4 sioannou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-April 11

Re: Multithreading _pass by reference

Posted 09 April 2011 - 01:41 PM

JackOfAllTrades thanks for yours try to help however could you please be a little more specific? I mean I did not understand your posted code. So my question remains, is there an easy way to pass a variable

 ThreadStart ts = new ThreadStart(calc[i].calculate_product);


via reference, or at least make calculate_product() function returns an array of integers?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1