CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

UDF parallelization issue

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 18, 2024, 23:52
Default UDF parallelization issue
  #1
New Member
 
ttg
Join Date: Jul 2022
Posts: 1
Rep Power: 0
Tony G is on a distinguished road
Dear all
My code runs well in serial mode, while after parallelization, the solver freeze after clicking on calculation.
Here are the codes:
#include "udf.h"
#include <stdio.h>
#include <winsock2.h>
#include <winsock.h>
#pragma comment(lib,"ws2_32.lib")

// Fluent sends this array
double send_data[4] = {500.0, 500.0, 500.0, 500.0};
double recvbuf[4] = {0.0}; // Make recvbuf global

double fluentUDP1(double senddata[]) // Send array
{

Message("fluentUDP called with send_data = [%f, %f, %f, %f]\n",
senddata[0], senddata[1], senddata[2], senddata[3]);

// Initialize Winsock
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
Message("WSAStartup failed.\n");
return -1;
}
Message("WSAStartup successful.\n");

// Create socket
SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == INVALID_SOCKET)
{
Message("Socket creation failed. Error code: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
Message("Socket created successfully.\n");

// Bind to a local port (ensure it's not in use)
struct sockaddr_in local_addr;
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
local_addr.sin_port = htons(8111); //

if (bind(sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) == SOCKET_ERROR)
{
Message("Bind failed with error code: %d\n", WSAGetLastError());//数值正常时可忽略报错,调试使用
closesocket(sockfd);
WSACleanup();
return -1;
}
Message("Bind successful on 127.0.0.1:%d.\n", ntohs(local_addr.sin_port));

// Set up the remote address (MATLAB's listening port)
struct sockaddr_in sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(4911); // MATLAB's LocalPort
sock_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

// Set receive timeout
int timeout = 60000; // milliseconds
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) == SOCKET_ERROR)
{
Message("Failed to set socket receive timeout. Error code: %d\n", WSAGetLastError());
closesocket(sockfd);
WSACleanup();
return -1;
}
Message("Receive timeout set to %d ms.\n", timeout);

// Send data to MATLAB

int dataSize = sizeof(double) * 4; // Size of data to send/receive

if (sendto(sockfd, (char *)senddata, dataSize, 0,
(struct sockaddr*)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR)
{
Message("sendto failed. Error code: %d\n", WSAGetLastError());
closesocket(sockfd);
WSACleanup();
return -1;
}
Message("senddata sent to MATLAB.\n");

// Receive data from MATLAB
if (recvfrom(sockfd, (char *)recvbuf, dataSize, 0, NULL, NULL) == SOCKET_ERROR)
{
Message("recvfrom failed. Error code: %d\n", WSAGetLastError());
closesocket(sockfd);
WSACleanup();
return -1;
}
Message("recvbuf = [%f, %f, %f, %f] received from MATLAB.\n",
recvbuf[0], recvbuf[1], recvbuf[2], recvbuf[3]);

// Clean up
closesocket(sockfd);
WSACleanup();
Message("fluentUDP completed successfully.\n");

}

DEFINE_EXECUTE_AT_END(get_array_from_fluent)
{
const char *report_names[4] = {"h1", "h2", "h3", "h4"};
int nrOfvalues;
real *values;
int *ids;
int index;
int rv;
#if RP_HOST
for (int i = 0; i < 4; i++)
{

const char *report_name = report_names[i];
/* First call to get number of values */
rv = Get_Report_Definition_Values(report_name, 1, &nrOfvalues, NULL, NULL, NULL);
if (rv == 0 && nrOfvalues > 0)
{
/* Allocate memory */
values = (real*) malloc(sizeof(real) * nrOfvalues);
ids = (int*) malloc(sizeof(int) * nrOfvalues);
if (values == NULL || ids == NULL)
{
Message("Memory allocation failed.\n");
if (values) free(values);
if (ids) free(ids);
return;
}
/* Second call to get values */
rv = Get_Report_Definition_Values(report_name, 1, NULL, values, ids, &index);
if (rv != 0)
{
Message("Error retrieving report '%s'. rv = %d\n", report_name, rv);
free(values);
free(ids);
return;
}
send_data[i] = values[0]; // Assuming we want the first value
Message("send_data[%d] = %f from report '%s'.\n", i, send_data[i], report_name);
free(values);
free(ids);
}
else
{
// Handle error
Message("Failed to retrieve report '%s'. rv = %d\n", report_name, rv);
}
}

fluentUDP1(send_data); // Call the function to send and receive data
#endif

host_to_node_real(recvbuf,4);//received a fatal signal (SEGMENTATION VIOLATION) if removed

}
/
DEFINE_PROFILE(set_heat_flux1, thread, position)
{
host_to_node_real_1(recvbuf[0]);
#if RP_NODE

face_t f;


begin_f_loop(f, thread)
{
F_PROFILE(f, thread, position) = recvbuf[0];
}
end_f_loop(f, thread)
#endif


}

DEFINE_PROFILE(set_heat_flux2, thread, position)
{
host_to_node_real_1(recvbuf[1]);
#if RP_NODE

face_t f;



begin_f_loop(f, thread)
{
F_PROFILE(f, thread, position) = recvbuf[1];
}
end_f_loop(f, thread)
#endif


}

DEFINE_PROFILE(set_heat_flux3, thread, position)
{
host_to_node_real_1(recvbuf[2]);
#if RP_NODE

face_t f;


begin_f_loop(f, thread)
{
F_PROFILE(f, thread, position) = recvbuf[2];
}
end_f_loop(f, thread)
#endif


}

DEFINE_PROFILE(set_heat_flux4, thread, position)
{
host_to_node_real_1(recvbuf[3]);
#if RP_NODE

face_t f;


begin_f_loop(f, thread)
{
F_PROFILE(f, thread, position) = recvbuf[3];
}
end_f_loop(f, thread)
#endif


}
If host_to_node_real(recvbuf,4) is removed, the ncortex received a fatal signal (SEGMENTATION VIOLATION).
Any suggestion would be helpful
Tony G is offline   Reply With Quote

Reply

Tags
fluent, parallelization, udf


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
reaction UDF hooking issue !!! shafi_10 FLUENT 0 May 14, 2023 06:29
UDF Parabolic 2D Velocity Profile Issue chrislyn FLUENT 2 December 11, 2018 13:11
UDF issue Oula FLUENT 2 November 28, 2018 10:11
WILLING TO PAY/ FREELANCER REQUIRED / small UDF coding force loads over body / 6DOF acasas CFD Freelancers 1 January 23, 2015 08:26
Vibromixed 2d UDF loading issue carteblanche Main CFD Forum 0 May 23, 2011 15:42


All times are GMT -4. The time now is 14:54.