使用 win32 线程进行矩阵乘法

发布于 2024-11-08 17:54:43 字数 2243 浏览 1 评论 0原文

我不知道我的代码出了什么问题......它总是在所有元素中返回零。如果能提示一下问题出在哪里就太好了:)

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

int nGlobalCount = 0;
int thread_index = 0;
int num_of_thr=5;

int a[4][4], b[4][4], c[4][4];
int i, j, k;

struct v {
    int i; /*row*/
    int j; /*column*/
};

DWORD ThreadProc (LPVOID lpdwThreadParam ) {
    //
    struct v *input = (struct v *)lpdwThreadParam;
    int avg=4*4/num_of_thr;
    int count=0;

    for(int i = 0; i <= 3 ; i++) {
        for(int j = 0; j <= 3; j++) {
            int sum=0;
            for ( k = 0 ; k <= 3; k++) {
               sum=sum+((a[input->i][k])*(b[k][input->j]));
                c[input->i][input->j]=sum;
                count++;
            }
        }
    }

    //Print Thread Number
    //printf ("Thread #: %d\n", *((int*)lpdwThreadParam));
    //Reduce the count
    return 0;
}

int main() {
    //    int x=0;
    cout<<"enter no of threads : ";
    cin>>num_of_thr;
    DWORD ThreadIds[num_of_thr];
    HANDLE ThreadHandles[num_of_thr];
    //struct v {
    //    int i; /*row*/
    //    int j; /*column*/
    //};

    struct v data[num_of_thr];
    int i , j , k;

    for ( int i = 0 ; i <= 3; i++) {
        for (int j = 0 ; j <= 3 ; j++) {
            a[i][j] = rand() % 10;
            b[i][j] = rand() % 10;
            c[i][j] = 0;
        }
    }

    for(int i = 0; i < num_of_thr/2; i++) {
        for(int j = 0; j < num_of_thr/2; j++) {
            data[thread_index].i = i;
            data[thread_index].j = j;

            ThreadHandles[thread_index] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadProc, &data[thread_index], 0,&ThreadIds[thread_index]);

            thread_index++;
        }
    }

    WaitForMultipleObjects(num_of_thr, ThreadHandles, TRUE, INFINITE);
    cout<<"The resultant matrix is "<<endl;
    for ( i = 0 ; i < 4; i++) {
        for ( j = 0 ; j < 4 ; j++)
            cout<<c[i][j]<<" ";
        cout<<endl;
    }
    for (int i=0; i<num_of_thr; i++)
        CloseHandle(ThreadHandles[i]);
    return 0;
}

I have no idea what's wrong with my code ... It always return zeros in all the elements. A hint of where is the problem would be great :)

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

int nGlobalCount = 0;
int thread_index = 0;
int num_of_thr=5;

int a[4][4], b[4][4], c[4][4];
int i, j, k;

struct v {
    int i; /*row*/
    int j; /*column*/
};

DWORD ThreadProc (LPVOID lpdwThreadParam ) {
    //
    struct v *input = (struct v *)lpdwThreadParam;
    int avg=4*4/num_of_thr;
    int count=0;

    for(int i = 0; i <= 3 ; i++) {
        for(int j = 0; j <= 3; j++) {
            int sum=0;
            for ( k = 0 ; k <= 3; k++) {
               sum=sum+((a[input->i][k])*(b[k][input->j]));
                c[input->i][input->j]=sum;
                count++;
            }
        }
    }

    //Print Thread Number
    //printf ("Thread #: %d\n", *((int*)lpdwThreadParam));
    //Reduce the count
    return 0;
}

int main() {
    //    int x=0;
    cout<<"enter no of threads : ";
    cin>>num_of_thr;
    DWORD ThreadIds[num_of_thr];
    HANDLE ThreadHandles[num_of_thr];
    //struct v {
    //    int i; /*row*/
    //    int j; /*column*/
    //};

    struct v data[num_of_thr];
    int i , j , k;

    for ( int i = 0 ; i <= 3; i++) {
        for (int j = 0 ; j <= 3 ; j++) {
            a[i][j] = rand() % 10;
            b[i][j] = rand() % 10;
            c[i][j] = 0;
        }
    }

    for(int i = 0; i < num_of_thr/2; i++) {
        for(int j = 0; j < num_of_thr/2; j++) {
            data[thread_index].i = i;
            data[thread_index].j = j;

            ThreadHandles[thread_index] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadProc, &data[thread_index], 0,&ThreadIds[thread_index]);

            thread_index++;
        }
    }

    WaitForMultipleObjects(num_of_thr, ThreadHandles, TRUE, INFINITE);
    cout<<"The resultant matrix is "<<endl;
    for ( i = 0 ; i < 4; i++) {
        for ( j = 0 ; j < 4 ; j++)
            cout<<c[i][j]<<" ";
        cout<<endl;
    }
    for (int i=0; i<num_of_thr; i++)
        CloseHandle(ThreadHandles[i]);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-11-15 17:54:43

乍一看,循环中的总和声明看起来很粗略。

for(int i = 0; i <= 3 ; i++) {
    for(int j = 0; j <= 3; j++) {
        for ( k = 0 ; k <= 3; k++)

            {
            int sum=sum+((a[input->i][k])*(b[k][input->j])); // this declaration seems wrong
            c[input->i][input->j]=sum;
            count++;
            }
        }
    }

您重新声明 sum 的每个内部循环,实际上使其变为 0。您可能希望将声明从赋值向上移动一到两个循环,具体取决于您想要实现的目标。

At a GLANCE, your sum declaration in the loop looks sketchy.

for(int i = 0; i <= 3 ; i++) {
    for(int j = 0; j <= 3; j++) {
        for ( k = 0 ; k <= 3; k++)

            {
            int sum=sum+((a[input->i][k])*(b[k][input->j])); // this declaration seems wrong
            c[input->i][input->j]=sum;
            count++;
            }
        }
    }

Each inner loop you redeclare sum, effectively making it 0. You might want to move the declaration up one or two loops from the assignment depending on what you are trying to achieve.

赤濁 2024-11-15 17:54:43

您是否意识到有两组独立的变量,分别名为 a、b 和 c?一个是 main 函数的本地函数,另一个是整个程序的静态函数。我怀疑这不是你想要的。尝试删除主本地的那个。

马丁

Do you realise that you have two separate sets of variables named a, b and c? One is local to the function main, and the other is a static for the whole program. I suspect that this is not what you intended. Try deleting the one that is local to main.

Martyn

你曾走过我的故事 2024-11-15 17:54:43

除了前面提到的其他问题之外,我在研究时发现了一些事情:

  • 你用什么来编译这个?在 VC++ 2010 中,它可以“工作”,因为它输出非零值,尽管它抱怨 DWORD ThreadIds[num_of_thr]; 数组声明具有非恒定数组大小(我刚刚制作了 num_of_thr 一个常量并注释掉 cin 以快速测试它)。
  • 您确定使用 cin >> 输入有效的线程数吗? num_of_thr; 例如,如果 num_of_thr 为 0,这将解释零输出。这里的 num_of_thr 的简单 cout 会很有用。
  • 在以 for(int i = 0; i < num_of_thr/2; i++) { 开头的数据初始化循环中,您没有正确计算线程数,这将导致数组下溢或溢出。例如,如果 num_of_thr 为 5,则 num_of_thr/2 为 2,这会导致仅初始化元素 0..3,而最后一个元素未初始化。数组下溢在技术上是可以的,尽管稍后的 CloseHandle() 调用在尝试释放本质上随机的句柄时会失败。如果您输入较大数量的线程,您将溢出所有数组(例如尝试使用 num_of_thr=10)。
  • 如果仍然不起作用,请尝试删除线程以查看线程或代码本身是否是问题的根源。例如,您可以在循环中手动调用 ThreadProc() 函数,而不是在线程内调用。使用调试器跟踪程序或将日志输出到 stdout/文件(这也适用于线程模型)。
  • 我首先会使用一些具有已知结果的固定值,而不是随机源矩阵。这将使您更容易确定代码是否确实计算了正确的结果。

A few things I found while poking about in addition to the other issues noted previously:

  • What are you compiling this with? With VC++ 2010 it "works", as in it outputs non-zeroes, although it complains about the DWORD ThreadIds[num_of_thr]; array declaration with a non-constant array size (I just made num_of_thr a constant and commented out the cin to test it quickly).
  • Are you sure you are inputting a valid number of threads with cin >> num_of_thr; For example, if num_of_thr was 0 this would explain the zeroes output. A simple cout here for num_of_thr would be useful.
  • In your data initialization loop starting with for(int i = 0; i < num_of_thr/2; i++) { you are not correctly counting threads which will result in an array underflow or overflow. For example, if num_of_thr is 5 then num_of_thr/2 is 2 which results in initializing only the elements 0..3 leaving the last element uninitialized. An array underflow is technically ok although the later CloseHandle() call will fail when it tries to free an essentially random handle. If you enter a larger number of threads you will overflow all your arrays (try it with num_of_thr=10 for example).
  • If it still doesn't work try removing the threading to see if the threading or code itself is the source of the problem. For example, you can call the ThreadProc() function manually in a loop instead of from within threads. Either trace through the program with a debugger or output logs to stdout/file (which would also work in the threading model).
  • Instead of a random source matrix I would use a few fixed values at first with a known result. This will make it easier to determine if the code is actually computing the correct result.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文