使用 Openmp 并行化 c 程序

时间:2021-04-20 08:45:32

标签: c openmp

我是 openmp 库的初学者,我想尝试并行化我的 c 代码的这一部分,但实际上我没有看到有关如何并行化用户动态响应部分的文档。当我尝试使用:

int main (int argc, char** argv)
{

    omp_set_num_threads(NUM_THREADS);


    #pragma omp parallel

    {

     // gets user's input value
    int length = 100; //initial size
    char * keyVal = malloc(length * sizeof(char)); //allocate mem for 100 chars
    char * ivVal = malloc(length * sizeof(char)); //allocate mem for 100 chars

    char * cipherVal = malloc(length * sizeof(char)); //allocate mem for 100 chars
    int count = 0; //to keep track of how many chars have been used
    char c; // to store the current char

    printf("\n  Enter your key value : ");
    gets(keyVal);

    printf("\n  Enter your iv value : ");
    gets(ivVal);

    printf("\n  Enter your ciphertext : ");
    gets(cipherVal);

    while((c = getchar()) != '\n'){ //keep reading until a newline
    if(count >= length)
        keyVal = realloc(keyVal, (length += 10) * sizeof(char)); //add room for 10 more chars
        ivVal = realloc(ivVal, (length += 10) * sizeof(char)); //add room for 10 more chars
        cipherVal = realloc(cipherVal, (length += 10) * sizeof(char)); //add room for 10 more chars
        // plainValue = realloc(plainValue, (length += 10) * sizeof(char)); //add room for 10 more chars
    keyVal[count++] = c;
    ivVal[count++] = c;
    cipherVal[count++] = c;
 
    }

    time_t begin = time(NULL);

    /* A 128 bit key */
    unsigned char *key = keyVal;

    /* A 128 bit IV */
    unsigned char *iv = ivVal;

    unsigned char *ciphertext=cipherVal;


    /* Message to be encrypted */
    unsigned char *plaintext =(unsigned char *)"This is the top secret message in parallel computing! Please keep it in a safe place.";

    /*
     * Buffer for ciphertext. Ensure the buffer is long enough for the
     * ciphertext which may be longer than the plaintext, depending on the
     * algorithm and mode.
     */

    /* Buffer for the decrypted text */
    unsigned char decryptedtext[128];

    int decryptedtext_len, ciphertext_len;

    /* Encrypt the plaintext */
    ciphertext_len = encrypt (plaintext, strlen ((char *)plaintext), key, iv,
                              ciphertext);



    /* Do something useful with the ciphertext here */
    printf("Ciphertext is:\n");
    BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);

    /* Decrypt the ciphertext */
    decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
                                decryptedtext);

    /* Add a NULL terminator. We are expecting printable text */
    decryptedtext[decryptedtext_len] = '\0';

    /* Show the decrypted text */
    printf("Decrypted text is:\n");
    printf("%s\n", decryptedtext);

    time_t end = time(NULL);
            printf("Time elpased is %ld seconds", (end - begin));
    }
}

它显示如下输出:

enter image description here

但是,我实际上希望每个值只出现一次提示。请帮助我,因为我已经分别检查了谷歌,看看我出了什么问题,但没有结果。

1 个答案:

答案 0 :(得分:0)

工作(给出与串行版本相同的输出)并行版本是: int main (int argc, char** argv) {

    omp_set_num_threads(NUM_THREADS);

    time_t begin = time(NULL);

    // gets user's input value
    int length = 100; //initial size
    char * keyVal = malloc(length * sizeof(char)); //allocate mem for 100 chars
    char * ivVal = malloc(length * sizeof(char)); //allocate mem for 100 chars

    char * cipherVal = malloc(length * sizeof(char)); //allocate mem for 100 chars
    int count = 0; //to keep track of how many chars have been used
    char c; // to store the current char

    /* A 128 bit key */
    unsigned char *key = keyVal;

    /* A 128 bit IV */
    unsigned char *iv = ivVal;

    unsigned char *ciphertext=cipherVal;

    int decryptedtext_len, ciphertext_len;

    /* Buffer for the decrypted text */
    unsigned char decryptedtext[128];

    printf("\n  Enter your key value : ");
    gets(keyVal);

    printf("\n  Enter your iv value : ");
    gets(ivVal);

    printf("\n  Enter your ciphertext : ");
    gets(cipherVal);


    #pragma omp parallel

    {

    while((c = getchar()) != '\n'){ //keep reading until a newline
    if(count >= length)
        keyVal = realloc(keyVal, (length += 10) * sizeof(char)); //add room for 10 more chars
        ivVal = realloc(ivVal, (length += 10) * sizeof(char)); //add room for 10 more chars
        cipherVal = realloc(cipherVal, (length += 10) * sizeof(char)); //add room for 10 more chars
        // plainValue = realloc(plainValue, (length += 10) * sizeof(char)); //add room for 10 more chars
    keyVal[count++] = c;
    ivVal[count++] = c;
    cipherVal[count++] = c;
 
    }


        #pragma omp critical
        {
            /* Message to be encrypted */
            unsigned char *plaintext =(unsigned char *)"This is the top secret message in parallel computing! Please keep it in a safe place.";

            /*
            * Buffer for ciphertext. Ensure the buffer is long enough for the
            * ciphertext which may be longer than the plaintext, depending on the
            * algorithm and mode.
            */

            /* Encrypt the plaintext */
            ciphertext_len = encrypt (plaintext, strlen ((char *)plaintext), key, iv,
                                    ciphertext);
        }
    }

    /* Do something useful with the ciphertext here */
    printf("Ciphertext is:\n");
    BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);

    /* Decrypt the ciphertext */
    decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
                                decryptedtext);

    /* Add a NULL terminator. We are expecting printable text */
    decryptedtext[decryptedtext_len] = '\0';

    /* Show the decrypted text */
    printf("Decrypted text is:\n");
    printf("%s\n", decryptedtext);

    time_t end = time(NULL);
            printf("Time elpased is %ld seconds", (end - begin));
}
相关问题