Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:27:51+00:00 2026-06-04T11:27:51+00:00

I have this code and it crashes every 1-2th launch. I have tried use

  • 0

I have this code and it crashes every 1-2th launch.
I have tried use malloc/cudaMallocHost/cudeMalloc but it was useless. It think it happens due to manual cufftComplex initialization but prove it a can’t because without data I can’t get fft. Could you help me eliminate this crashes?

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <cuda.h>
#include <cufft.h>

using namespace std;

int main(int argc, char **argv)
{
cufftHandle plan;
cufftComplex *data;
cufftComplex *digits;
cufftComplex *h_data;

cudaMallocHost((void**)&digits, sizeof(cufftComplex)*8);
digits[0].x = 12.5f; digits[0].y = 0.0f;
digits[1].x = 66.23f; digits[1].y = 0.0f;
digits[2].x = 35.1f; digits[2].y = 0.0f;
digits[3].x = 16.7f; digits[3].y = 0.0f;
digits[4].x = 14.83f; digits[4].y = 0.0f;
digits[5].x = 55.1f; digits[5].y = 0.0f;
digits[6].x = 11.7f; digits[6].y = 0.0f;
digits[7].x = 18.83f; digits[7].y = 0.0f;

cudaMalloc((void**)&data, sizeof(cufftComplex)*8);

cudaMemcpy(data, digits, sizeof(cufftComplex)*8, cudaMemcpyHostToDevice);

if (cufftPlan1d(&plan, 8, CUFFT_C2C, 1) != CUFFT_SUCCESS) {
    fprintf(stderr, "Cuda: cufftPlan1d CUFFT_C2C failed\n");
    return 1;
}

if (cufftExecC2C(plan, data, data, CUFFT_FORWARD) != CUFFT_SUCCESS) {
    fprintf(stderr, "Cuda: cufftExecC2C CUFFT_FORWARD failed\n");
    return 1;
}

if (cudaMalloc((void**)&h_data, sizeof(cufftComplex)*8) != cudaSuccess) {
    fprintf(stderr, "Cuda: cudaMalloc((void**)&h_data failed\n");
    return 1;
}

cudaMemcpy(h_data, data, sizeof(cufftComplex)*8, cudaMemcpyDeviceToHost);


printf("\nOriginal:\n");
for(int i = 0; i < 8; ++i){
    printf("\nRe:%2.5f   Im:%2.5f", digits[i].x, digits[i].y);
} 

printf("\n\n1D-FFT:\n");
for(int i = 0; i < 8; ++i){
    printf("\nRe:%2.5f   Im:%2.5f", h_data[i].x, h_data[i].y);
} 

cudaFree(digits);
cudaFree(data);
cudaFree(h_data);
cufftDestroy(plan);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:27:52+00:00Added an answer on June 4, 2026 at 11:27 am

    You basic problem is improper mixing of host and device memory pointers. You have assigned the address of a device memory allocation (using cudaMalloc) to h_data , but are trying to use it as a pointer to an address in host memory. That won’t work and is producing the host segmentation fault you are seeing. Your example should look something like:

    #include <cstdlib>
    #include <cuda_runtime.h>
    #include <cufft.h>
    
    int main(int argc, char **argv)
    {
        cufftHandle plan;
        cufftComplex *data, *digits, *h_data;
    
        digits = (cufftComplex *)malloc(sizeof(cufftComplex)*8);
        digits[0].x = 12.5f; digits[0].y = 0.0f;
        digits[1].x = 66.23f; digits[1].y = 0.0f;
        digits[2].x = 35.1f; digits[2].y = 0.0f;
        digits[3].x = 16.7f; digits[3].y = 0.0f;
        digits[4].x = 14.83f; digits[4].y = 0.0f;
        digits[5].x = 55.1f; digits[5].y = 0.0f;
        digits[6].x = 11.7f; digits[6].y = 0.0f;
        digits[7].x = 18.83f; digits[7].y = 0.0f;
    
        cudaMalloc((void**)&data, sizeof(cufftComplex)*8);
        cudaMemcpy(data, digits, sizeof(cufftComplex)*8, cudaMemcpyHostToDevice);
    
        if (cufftPlan1d(&plan, 8, CUFFT_C2C, 1) != CUFFT_SUCCESS) {
            fprintf(stderr, "Cuda: cufftPlan1d CUFFT_C2C failed\n");
            return 1;
        }
    
        if (cufftExecC2C(plan, data, data, CUFFT_FORWARD) != CUFFT_SUCCESS) {
            fprintf(stderr, "Cuda: cufftExecC2C CUFFT_FORWARD failed\n");
            return 1;
        }
    
        h_data = (cufftComplex *)malloc(sizeof(cufftComplex)*8);
        cudaMemcpy(h_data, data, sizeof(cufftComplex)*8, cudaMemcpyDeviceToHost);
    
        printf("\nOriginal:\n");
        for(int i = 0; i < 8; ++i){
            printf("\nRe:%2.5f   Im:%2.5f", digits[i].x, digits[i].y);
        } 
    
        printf("\n\n1D-FFT:\n");
        for(int i = 0; i < 8; ++i){
            printf("\nRe:%2.5f   Im:%2.5f", h_data[i].x, h_data[i].y);
        } 
    
        free(digits);
        free(h_data);
        cudaFree(data);
        cufftDestroy(plan);
    }
    

    Note that you should use plain malloc or the C++ new operator to allocate host side memory rather than cudaMallocHost, unless you understand very well what the latter API does and why you are using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code but every time i run the app and click on
What's the problem with this code? It crashes every time. One time it's a
I have this code for doing an ajax request to a webservice: var MyCode
I have this code: $(div[id^='intCell']).mouseover(function() { $(this).css({ border:,1px solid #ff097c}); }).mouseout(function() { $(this).css({border:,1px solid
I have this code to show a map using the Virtual Earth API: <script
I have this code here, which is intended to allow any type of arguments:
I have this code in my forms.py : from django import forms from formfieldset.forms
I have this code so I can grab the values ind the dataset before
I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray
I have this code private void writeReport(IReport report, string reportName) { string reportString =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.