PDA

View Full Version : مشکل در استفاده از Parallel.For



13601360
پنج شنبه 07 اردیبهشت 1391, 20:07 عصر
سلام دوستان
من برای ضرب دو ماتریس از Parallel.For استفاده می کنم
اما موقع اجرا خطا می ده
متن خطا




System.AggregateException: One or more errors occurred. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Matrix.Program.<>c__DisplayClass1.<Main>b__0(Int32 ii) in c:\Users\Ali\Documents\Sharp Develop\Matrix\Program.cs:line 46
at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Tas k childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass7.<ExecuteSelfReplicating>b__6(Object )
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boo lean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, Action`1 body)
at Matrix.Program.Main(String[] args) in c:\Users\Ali\Documents\Sharp Develop\Matrix\Program.cs:line 39




این کد برنامه


int n = int.Parse(Console.ReadLine());
int i, j , k, temp;

int[,] a = new int[n, n];
int[,] b = new int[n, n];
int[,] c = new int[n, n];

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = 1;
b[i, j] = 2;
c[i, j] = 0;
}
}

Parallel.For(0, n, ii =>
{
for (j = 0; j < n; j++)
{
temp = 0;
for (k = 0; k < n; k++)
{
temp += a[i, k] * b[k, j];
}
c[i, j] = temp;
}
});

massar
پنج شنبه 07 اردیبهشت 1391, 21:12 عصر
سلام
فک کنم منظورتون این بوده که تو حلقه Parallel.For به جای i از ii استفاده کنید.


Parallel.For(0, n, ii =>
{
for (j = 0; j < n; j++)
{
temp = 0;
for (k = 0; k < n; k++)
{
temp += a[ii, k] * b[k, j];
}
c[ii, j] = temp;
}
});