Cfish: Function call problem (segmentation fault)

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Cfish: Function call problem (segmentation fault)

Post by Look »

To be more precise , I want this "pos->materialTable->gamePhase" to be correctly assigned it's right value.
Farewell.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Cfish: Function call problem (segmentation fault)

Post by Look »

I managed to get gamePhase correctly. By defining and using a global variable in evaluate.c file. But unfortunately now a similar way would not work for kingSafety.

evaluate.c :

Code: Select all

[CODE1]

Position g_pos;

[CODE2]

// evaluate_classical() is the classical evaluation function. It returns
// a static evaluation of the position from the point of view of the side
// to move.

/*static Value*/Value evaluate_classical(const Position *pos) // [MA]
{
  assert(!checkers());

  Score mobility[2] = { SCORE_ZERO, SCORE_ZERO };
  Value v;
  EvalInfo ei;
    
  // Probe the material hash table
  ei.me = material_probe(pos);
	
  // If we have a specialized evaluation function for the current material
  // configuration, call it and return.
  if (material_specialized_eval_exists(ei.me)) // [MA]
  {
  	//VALUE_PHASE vph={.value=material_evaluate(ei.me, pos) , .phase=ei.me->gamePhase};
    return material_evaluate(ei.me, pos);//vph;
  }
  // Initialize score by reading the incrementally updated scores included
  // in the position struct (material + piece square tables) and the
  // material imbalance. Score is computed internally from the white point
  // of view.
  Score score = psq_score() + material_imbalance(ei.me) + pos->contempt;

  // Probe the pawn hash table
  ei.pe = pawn_probe(pos);
  score += ei.pe->score;

  // Early exit if score is high
#define lazy_skip(v) (abs(mg_value(score) + eg_value(score)) / 2 > v + non_pawn_material() / 64)
  if (lazy_skip(LazyThreshold1))
    goto make_v;

  // Initialize attack and king safety bitboards.
  evalinfo_init(pos, &ei, WHITE);
  evalinfo_init(pos, &ei, BLACK);
  
  // Evaluate all pieces but king and pawns
  score +=  evaluate_pieces(pos, &ei, mobility, WHITE, KNIGHT)
          - evaluate_pieces(pos, &ei, mobility, BLACK, KNIGHT)
          + evaluate_pieces(pos, &ei, mobility, WHITE, BISHOP)
          - evaluate_pieces(pos, &ei, mobility, BLACK, BISHOP)
          + evaluate_pieces(pos, &ei, mobility, WHITE, ROOK)
          - evaluate_pieces(pos, &ei, mobility, BLACK, ROOK)
          + evaluate_pieces(pos, &ei, mobility, WHITE, QUEEN)
          - evaluate_pieces(pos, &ei, mobility, BLACK, QUEEN);

  score += mobility[WHITE] - mobility[BLACK];

  // Evaluate kings after all other pieces because we need full attack
  // information when computing the king safety evaluation.
  score +=  evaluate_king(pos, &ei, mobility, WHITE)
          - evaluate_king(pos, &ei, mobility, BLACK);

  // Evaluate passed pawns, we need full attack information including king
  score +=  evaluate_passed(pos, &ei, WHITE)
          - evaluate_passed(pos, &ei, BLACK);

  if (lazy_skip(LazyThreshold2))
    goto make_v;

  // Evaluate tactical threats, we need full attack information including king
  score +=  evaluate_threats(pos, &ei, WHITE)
          - evaluate_threats(pos, &ei, BLACK);

  // Evaluate space for both sides, only during opening
  score +=  evaluate_space(pos, &ei, WHITE)
          - evaluate_space(pos, &ei, BLACK);

make_v:
  // Derive single value from the mg and eg parts of the score
  v = evaluate_winnable(pos, &ei, score);

  // Evaluation grain
  v = (v / 16) * 16;

  // Side to move point of view
  v = (stm() == WHITE ? v : -v) + Tempo;

	 g_pos=*pos;
	 g_pos.materialTable->gamePhase=ei.me->gamePhase;
	 g_pos.pawnTable->kingSafety[WHITE]=ei.pe->kingSafety[WHITE];
	 g_pos.pawnTable->kingSafety[BLACK]=ei.pe->kingSafety[BLACK];
	 
  return v;
}

[CODE3]
I get values like -786450 , -786383 for kingSafety WHITE , BLACK.
Farewell.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Cfish: Function call problem (segmentation fault)

Post by Look »

I changed this function , but still getting gibberish scores.

Code: Select all

void pawn_entry_fill(const Position *pos, PawnEntry *e, Key key) 
{
  e->key = key;
  e->blockedCount = 0;
  e->score = pawn_evaluate(pos, e, WHITE) - pawn_evaluate(pos, e, BLACK);
  e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]);
  e->passedCount = popcount(e->passedPawns[WHITE] | e->passedPawns[BLACK]);
  
  Square wksq =square_of(WHITE, KING);
  assert(pos->byTypeBB[KING] & pos->byColorBB[WHITE] & sq_bb(wksq));
  e->kingSafety[WHITE]=do_king_safety_white(e,pos,wksq); 
  
  Square bksq =square_of(BLACK, KING);
  assert(pos->byTypeBB[KING] & pos->byColorBB[BLACK] & sq_bb(bksq));
  e->kingSafety[BLACK]=do_king_safety_black(e,pos,bksq); 
}
Farewell.
Dann Corbit
Posts: 12537
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Cfish: Function call problem (segmentation fault)

Post by Dann Corbit »

Seems to me that a sensible approach would be to clone the search function and have it perform a simple evaluation.
That way it is the proper thread and everything should be initialized properly
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Cfish: Function call problem (segmentation fault)

Post by Look »

I need to learn more about threads now. Later I may work on search clone.
Farewell.
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Cfish: Function call problem (segmentation fault)

Post by Look »

My mistake. Those gibberish numbers were Scores. After separating them into mg_value , eg_value I have the correct numbers. Now I need to study how exactly these Scores/Values are computed.
Farewell.
Dann Corbit
Posts: 12537
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Cfish: Function call problem (segmentation fault)

Post by Dann Corbit »

I am glad to see that you are making progress.
I wish you success on your project.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.